home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 6 / CU Amiga Magazine's Super CD-ROM 06 (1996)(EMAP Images)(GB)(Track 1 of 4)[!][issue 1997-01].iso / cucd / prog / gnu-c / src / gcc-2.7.0-amiga / cp / pt.c < prev    next >
C/C++ Source or Header  |  1995-06-15  |  75KB  |  2,692 lines

  1. /* Handle parameterized types (templates) for GNU C++.
  2.    Copyright (C) 1992, 1993 Free Software Foundation, Inc.
  3.    Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
  4.  
  5. This file is part of GNU CC.
  6.  
  7. GNU CC is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11.  
  12. GNU CC is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU CC; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 59 Temple Place - Suite 330,
  20. Boston, MA 02111-1307, USA.  */
  21.  
  22. /* Known bugs or deficiencies include:
  23.    * templates for class static data don't work (methods only)
  24.    * duplicated method templates can crash the compiler
  25.    * interface/impl data is taken from file defining the template
  26.    * all methods must be provided in header files; can't use a source
  27.      file that contains only the method templates and "just win"
  28.    * method templates must be seen before the expansion of the
  29.      class template is done
  30.  */
  31.  
  32. #include "config.h"
  33. #include <stdio.h>
  34. #include "obstack.h"
  35.  
  36. #include "tree.h"
  37. #include "flags.h"
  38. #include "cp-tree.h"
  39. #include "decl.h"
  40. #include "parse.h"
  41. #include "lex.h"
  42. #include "output.h"
  43. #include "defaults.h"
  44.  
  45. extern struct obstack permanent_obstack;
  46. extern tree grokdeclarator ();
  47.  
  48. extern int lineno;
  49. extern char *input_filename;
  50. struct pending_inline *pending_template_expansions;
  51.  
  52. int processing_template_decl;
  53. int processing_template_defn;
  54.  
  55. #define obstack_chunk_alloc xmalloc
  56. #define obstack_chunk_free free
  57.  
  58. static int unify ();
  59. static void add_pending_template ();
  60.  
  61. void overload_template_name (), pop_template_decls ();
  62.  
  63. /* We've got a template header coming up; set obstacks up to save the
  64.    nodes created permanently.  (There might be cases with nested templates
  65.    where we don't have to do this, but they aren't implemented, and it
  66.    probably wouldn't be worth the effort.)  */
  67. void
  68. begin_template_parm_list ()
  69. {
  70.   pushlevel (0);
  71.   push_obstacks (&permanent_obstack, &permanent_obstack);
  72.   pushlevel (0);
  73. }
  74.  
  75. /* Process information from new template parameter NEXT and append it to the
  76.    LIST being built.  The rules for use of a template parameter type name
  77.    by later parameters are not well-defined for us just yet.  However, the
  78.    only way to avoid having to parse expressions of unknown complexity (and
  79.    with tokens of unknown types) is to disallow it completely.    So for now,
  80.    that is what is assumed.  */
  81. tree
  82. process_template_parm (list, next)
  83.      tree list, next;
  84. {
  85.   tree parm;
  86.   tree decl = 0;
  87.   tree defval;
  88.   int is_type;
  89.   parm = next;
  90.   my_friendly_assert (TREE_CODE (parm) == TREE_LIST, 259);
  91.   defval = TREE_PURPOSE (parm);
  92.   parm = TREE_VALUE (parm);
  93.   is_type = TREE_PURPOSE (parm) == class_type_node;
  94.   if (!is_type)
  95.     {
  96.       tree tinfo = 0;
  97.       my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm)) == TREE_LIST, 260);
  98.       /* is a const-param */
  99.       parm = grokdeclarator (TREE_VALUE (parm), TREE_PURPOSE (parm),
  100.                  PARM, 0, NULL_TREE);
  101.       /* A template parameter is not modifiable.  */
  102.       TREE_READONLY (parm) = 1;
  103.       if (IS_AGGR_TYPE (TREE_TYPE (parm)))
  104.     {
  105.       sorry ("aggregate template parameter types");
  106.       TREE_TYPE (parm) = void_type_node;
  107.     }
  108.       tinfo = make_node (TEMPLATE_CONST_PARM);
  109.       my_friendly_assert (TREE_PERMANENT (tinfo), 260.5);
  110.       if (TREE_PERMANENT (parm) == 0)
  111.         {
  112.       parm = copy_node (parm);
  113.       TREE_PERMANENT (parm) = 1;
  114.         }
  115.       TREE_TYPE (tinfo) = TREE_TYPE (parm);
  116.       decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
  117.       DECL_INITIAL (decl) = tinfo;
  118.       DECL_INITIAL (parm) = tinfo;
  119.     }
  120.   else
  121.     {
  122.       tree t = make_node (TEMPLATE_TYPE_PARM);
  123.       decl = build_decl (TYPE_DECL, TREE_VALUE (parm), t);
  124.       TYPE_MAIN_DECL (t) = decl;
  125.       parm = decl;
  126.       if (defval)
  127.     {
  128.       if (IDENTIFIER_HAS_TYPE_VALUE (defval))
  129.         defval = IDENTIFIER_TYPE_VALUE (defval);
  130.       else
  131.         defval = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (defval));
  132.     }
  133.     }
  134.   SET_DECL_ARTIFICIAL (decl);
  135.   pushdecl (decl);
  136.   parm = build_tree_list (defval, parm);
  137.   return chainon (list, parm);
  138. }
  139.  
  140. /* The end of a template parameter list has been reached.  Process the
  141.    tree list into a parameter vector, converting each parameter into a more
  142.    useful form.     Type parameters are saved as IDENTIFIER_NODEs, and others
  143.    as PARM_DECLs.  */
  144.  
  145. tree
  146. end_template_parm_list (parms)
  147.      tree parms;
  148. {
  149.   int nparms = 0;
  150.   int saw_default = 0;
  151.   tree saved_parmlist;
  152.   tree parm;
  153.   for (parm = parms; parm; parm = TREE_CHAIN (parm))
  154.     nparms++;
  155.   saved_parmlist = make_tree_vec (nparms);
  156.  
  157.   for (parm = parms, nparms = 0; parm; parm = TREE_CHAIN (parm), nparms++)
  158.     {
  159.       tree p = TREE_VALUE (parm);
  160.       if (TREE_PURPOSE (parm))
  161.     saw_default = 1;
  162.       else if (saw_default)
  163.     {
  164.       error ("if a default argument is given for one template parameter");
  165.       error ("default arguments must be given for all subsequent");
  166.       error ("parameters as well");
  167.     }
  168.  
  169.       if (TREE_CODE (p) == TYPE_DECL)
  170.     {
  171.       tree t = TREE_TYPE (p);
  172.       TEMPLATE_TYPE_SET_INFO (t, saved_parmlist, nparms);
  173.     }
  174.       else
  175.     {
  176.       tree tinfo = DECL_INITIAL (p);
  177.       DECL_INITIAL (p) = NULL_TREE;
  178.       TEMPLATE_CONST_SET_INFO (tinfo, saved_parmlist, nparms);
  179.     }
  180.       TREE_VEC_ELT (saved_parmlist, nparms) = parm;
  181.     }
  182.   set_current_level_tags_transparency (1);
  183.   processing_template_decl++;
  184.   return saved_parmlist;
  185. }
  186.  
  187. /* end_template_decl is called after a template declaration is seen.
  188.    D1 is template header; D2 is class_head_sans_basetype or a
  189.    TEMPLATE_DECL with its DECL_RESULT field set.  */
  190. void
  191. end_template_decl (d1, d2, is_class, defn)
  192.      tree d1, d2, is_class;
  193.      int defn;
  194. {
  195.   tree decl;
  196.   struct template_info *tmpl;
  197.  
  198.   tmpl = (struct template_info *) obstack_alloc (&permanent_obstack,
  199.                         sizeof (struct template_info));
  200.   tmpl->text = 0;
  201.   tmpl->length = 0;
  202.   tmpl->aggr = is_class;
  203.  
  204.   /* cloned from reinit_parse_for_template */
  205.   tmpl->filename = input_filename;
  206.   tmpl->lineno = lineno;
  207.   tmpl->parm_vec = d1;          /* [eichin:19911015.2306EST] */
  208.  
  209.   if (d2 == NULL_TREE || d2 == error_mark_node)
  210.     {
  211.       decl = 0;
  212.       goto lose;
  213.     }
  214.  
  215.   if (is_class)
  216.     {
  217.       decl = build_lang_decl (TEMPLATE_DECL, d2, NULL_TREE);
  218.       GNU_xref_decl (current_function_decl, decl);
  219.     }
  220.   else
  221.     {
  222.       if (TREE_CODE (d2) == TEMPLATE_DECL)
  223.     decl = d2;
  224.       else
  225.     {
  226.       /* Class destructor templates and operator templates are
  227.          slipping past as non-template nodes.  Process them here, since
  228.          I haven't figured out where to catch them earlier.  I could
  229.          go do that, but it's a choice between getting that done and
  230.          staying only N months behind schedule.  Sorry....  */
  231.       enum tree_code code;
  232.       my_friendly_assert (TREE_CODE (d2) == CALL_EXPR, 263);
  233.       code = TREE_CODE (TREE_OPERAND (d2, 0));
  234.       my_friendly_assert (code == BIT_NOT_EXPR
  235.           || code == OP_IDENTIFIER
  236.           || code == SCOPE_REF, 264);
  237.       d2 = grokdeclarator (d2, NULL_TREE, MEMFUNCDEF, 0, NULL_TREE);
  238.       decl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (d2),
  239.                   TREE_TYPE (d2));
  240.       DECL_TEMPLATE_RESULT (decl) = d2;
  241.       DECL_CONTEXT (decl) = DECL_CONTEXT (d2);
  242.       DECL_CLASS_CONTEXT (decl) = DECL_CLASS_CONTEXT (d2);
  243.       DECL_NAME (decl) = DECL_NAME (d2);
  244.       TREE_TYPE (decl) = TREE_TYPE (d2);
  245.       if (interface_unknown && flag_external_templates
  246.           && ! flag_alt_external_templates
  247.           && ! DECL_IN_SYSTEM_HEADER (decl))
  248.         warn_if_unknown_interface (decl);
  249.       TREE_PUBLIC (decl) = TREE_PUBLIC (d2) = flag_external_templates && !interface_unknown;
  250.       DECL_EXTERNAL (decl) = (DECL_EXTERNAL (d2)
  251.                   && !(DECL_CLASS_CONTEXT (d2)
  252.                        && !DECL_THIS_EXTERN (d2)));
  253.     }
  254.  
  255.       /* All routines creating TEMPLATE_DECL nodes should now be using
  256.      build_lang_decl, which will have set this up already.    */
  257.       my_friendly_assert (DECL_LANG_SPECIFIC (decl) != 0, 265);
  258.  
  259.       /* @@ Somewhere, permanent allocation isn't being used.  */
  260.       if (! DECL_TEMPLATE_IS_CLASS (decl)
  261.       && TREE_CODE (DECL_TEMPLATE_RESULT (decl)) == FUNCTION_DECL)
  262.     {
  263.       tree result = DECL_TEMPLATE_RESULT (decl);
  264.       /* Will do nothing if allocation was already permanent.  */
  265.       DECL_ARGUMENTS (result) = copy_to_permanent (DECL_ARGUMENTS (result));
  266.     }
  267.  
  268.       /* If this is for a method, there's an extra binding level here.    */
  269.       if (DECL_CONTEXT (DECL_TEMPLATE_RESULT (decl)) != NULL_TREE)
  270.     {
  271.       /* @@ Find out where this should be getting set!  */
  272.       tree r = DECL_TEMPLATE_RESULT (decl);
  273.       if (DECL_LANG_SPECIFIC (r) && DECL_CLASS_CONTEXT (r) == NULL_TREE)
  274.         DECL_CLASS_CONTEXT (r) = DECL_CONTEXT (r);
  275.     }
  276.     }
  277.   DECL_TEMPLATE_INFO (decl) = tmpl;
  278.   DECL_TEMPLATE_PARMS (decl) = d1;
  279.  
  280.   /* So that duplicate_decls can do the right thing.  */
  281.   if (defn)
  282.     DECL_INITIAL (decl) = error_mark_node;
  283.   
  284.   /* If context of decl is non-null (i.e., method template), add it
  285.      to the appropriate class template, and pop the binding levels.  */
  286.   if (! is_class && DECL_CONTEXT (DECL_TEMPLATE_RESULT (decl)) != NULL_TREE)
  287.     {
  288.       tree ctx = DECL_CONTEXT (DECL_TEMPLATE_RESULT (decl));
  289.       tree tmpl, t;
  290.       my_friendly_assert (TREE_CODE (ctx) == UNINSTANTIATED_P_TYPE, 266);
  291.       tmpl = UPT_TEMPLATE (ctx);
  292.       for (t = DECL_TEMPLATE_MEMBERS (tmpl); t; t = TREE_CHAIN (t))
  293.     if (TREE_PURPOSE (t) == DECL_NAME (decl)
  294.         && duplicate_decls (decl, TREE_VALUE (t)))
  295.       goto already_there;
  296.       DECL_TEMPLATE_MEMBERS (tmpl) =
  297.     perm_tree_cons (DECL_NAME (decl), decl, DECL_TEMPLATE_MEMBERS (tmpl));
  298.     already_there:
  299.       poplevel (0, 0, 0);
  300.       poplevel (0, 0, 0);
  301.     }
  302.   /* Otherwise, go back to top level first, and push the template decl
  303.      again there.  */
  304.   else
  305.     {
  306.       poplevel (0, 0, 0);
  307.       poplevel (0, 0, 0);
  308.       pushdecl (decl);
  309.     }
  310.  lose:
  311. #if 0 /* It happens sometimes, with syntactic or semantic errors.
  312.  
  313.      One specific case:
  314.      template <class A, int X, int Y> class Foo { ... };
  315.      template <class A, int X, int y> Foo<X,Y>::method (Foo& x) { ... }
  316.      Note the missing "A" in the class containing "method".  */
  317.   my_friendly_assert (global_bindings_p (), 267);
  318. #else
  319.   while (! global_bindings_p ())
  320.     poplevel (0, 0, 0);
  321. #endif
  322.   pop_obstacks ();
  323.   processing_template_decl--;
  324.   (void) get_pending_sizes ();
  325. }
  326.  
  327. /* If TYPE contains a template parm type, then substitute that type
  328.    with its actual type that is found in TVEC. */
  329. static void
  330. grok_template_type (tvec, type)
  331.      tree tvec;
  332.      tree* type;
  333. {
  334.   switch (TREE_CODE (*type))
  335.     {
  336.     case TEMPLATE_TYPE_PARM:
  337.       if (*type != TYPE_MAIN_VARIANT (*type))
  338.         {
  339.       /* we are here for cases like const T* etc. */
  340.       grok_template_type (tvec, &TYPE_MAIN_VARIANT (*type));
  341.       *type = cp_build_type_variant (TYPE_MAIN_VARIANT (*type),
  342.                     TYPE_READONLY (*type),
  343.                     TYPE_VOLATILE (*type));
  344.     }
  345.       else
  346.       *type = TREE_VEC_ELT (tvec, TEMPLATE_TYPE_IDX (*type));
  347.       return;
  348.     case POINTER_TYPE:
  349.     case REFERENCE_TYPE:
  350.       grok_template_type (tvec, &TREE_TYPE (*type));
  351.       return;
  352.     case FUNCTION_TYPE:
  353.       {
  354.     tree p;
  355.     
  356.     /* take care of function's return type first */
  357.     grok_template_type (tvec, &TREE_TYPE (*type));
  358.     
  359.     /* take care of function's arguments */
  360.     for (p = TYPE_ARG_TYPES (*type); p; p = TREE_CHAIN (p))
  361.       grok_template_type (tvec, &TREE_VALUE (p));
  362.     return;
  363.       }
  364.     default:     
  365.       break;
  366.     }
  367.   return;
  368. }
  369.  
  370. /* Convert all template arguments to their appropriate types, and return
  371.    a vector containing the resulting values.  If any error occurs, return
  372.    error_mark_node.  */
  373. static tree
  374. coerce_template_parms (parms, arglist, in_decl)
  375.      tree parms, arglist;
  376.      tree in_decl;
  377. {
  378.   int nparms, nargs, i, lost = 0;
  379.   tree vec;
  380.  
  381.   if (arglist == NULL_TREE)
  382.     nargs = 0;
  383.   else if (TREE_CODE (arglist) == TREE_VEC)
  384.     nargs = TREE_VEC_LENGTH (arglist);
  385.   else
  386.     nargs = list_length (arglist);
  387.  
  388.   nparms = TREE_VEC_LENGTH (parms);
  389.  
  390.   if (nargs > nparms
  391.       || (nargs < nparms
  392.       && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE))
  393.     {
  394.       error ("incorrect number of parameters (%d, should be %d)",
  395.          nargs, nparms);
  396.       if (in_decl)
  397.     cp_error_at ("in template expansion for decl `%D'", in_decl);
  398.       return error_mark_node;
  399.     }
  400.  
  401.   if (arglist && TREE_CODE (arglist) == TREE_VEC)
  402.     vec = copy_node (arglist);
  403.   else
  404.     {
  405.       vec = make_tree_vec (nparms);
  406.       for (i = 0; i < nparms; i++)
  407.     {
  408.       tree arg;
  409.  
  410.       if (arglist)
  411.         {
  412.           arg = arglist;
  413.           arglist = TREE_CHAIN (arglist);
  414.  
  415.           if (arg == error_mark_node)
  416.         lost++;
  417.           else
  418.         arg = TREE_VALUE (arg);
  419.         }
  420.       else
  421.         arg = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
  422.  
  423.       TREE_VEC_ELT (vec, i) = arg;
  424.     }
  425.     }
  426.   for (i = 0; i < nparms; i++)
  427.     {
  428.       tree arg = TREE_VEC_ELT (vec, i);
  429.       tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
  430.       tree val = 0;
  431.       int is_type, requires_type;
  432.  
  433.       is_type = TREE_CODE_CLASS (TREE_CODE (arg)) == 't';
  434.       requires_type = TREE_CODE (parm) == TYPE_DECL;
  435.       if (is_type != requires_type)
  436.     {
  437.       if (in_decl)
  438.         cp_error ("type/value mismatch in template parameter list for `%D'",
  439.               in_decl);
  440.       lost++;
  441.       TREE_VEC_ELT (vec, i) = error_mark_node;
  442.       continue;
  443.     }
  444.       if (is_type)
  445.     val = groktypename (arg);
  446.       else if (TREE_CODE (arg) == STRING_CST)
  447.     {
  448.       cp_error ("string literal %E is not a valid template argument", arg);
  449.       error ("because it is the address of an object with static linkage");
  450.       val = error_mark_node;
  451.     }
  452.       else
  453.     {
  454.       grok_template_type (vec, &TREE_TYPE (parm));
  455.       val = digest_init (TREE_TYPE (parm), arg, (tree *) 0);
  456.  
  457.       if (val == error_mark_node)
  458.         ;
  459.  
  460.       /* 14.2: Other template-arguments must be constant-expressions,
  461.          addresses of objects or functions with external linkage, or of
  462.          static class members.  */
  463.       else if (!TREE_CONSTANT (val))
  464.         {
  465.           cp_error ("non-const `%E' cannot be used as template argument",
  466.             arg);
  467.           val = error_mark_node;
  468.         }
  469.       else if (TREE_CODE (val) == ADDR_EXPR)
  470.         {
  471.           tree a = TREE_OPERAND (val, 0);
  472.           if ((TREE_CODE (a) == VAR_DECL
  473.            || TREE_CODE (a) == FUNCTION_DECL)
  474.           && ! DECL_PUBLIC (a))
  475.         {
  476.           cp_error ("address of non-extern `%E' cannot be used as template argument", a);
  477.           val = error_mark_node;
  478.         }
  479.         }
  480.     }
  481.  
  482.       if (val == error_mark_node)
  483.     lost++;
  484.  
  485.       TREE_VEC_ELT (vec, i) = val;
  486.     }
  487.   if (lost)
  488.     return error_mark_node;
  489.   return vec;
  490. }
  491.  
  492. /* Given class template name and parameter list, produce a user-friendly name
  493.    for the instantiation.  */
  494. static char *
  495. mangle_class_name_for_template (name, parms, arglist)
  496.      char *name;
  497.      tree parms, arglist;
  498. {
  499.   static struct obstack scratch_obstack;
  500.   static char *scratch_firstobj;
  501.   int i, nparms;
  502.  
  503.   if (!scratch_firstobj)
  504.     {
  505.       gcc_obstack_init (&scratch_obstack);
  506.       scratch_firstobj = obstack_alloc (&scratch_obstack, 1);
  507.     }
  508.   else
  509.     obstack_free (&scratch_obstack, scratch_firstobj);
  510.  
  511. #if 0
  512. #define buflen    sizeof(buf)
  513. #define check    if (bufp >= buf+buflen-1) goto too_long
  514. #define ccat(c) *bufp++=(c); check
  515. #define advance    bufp+=strlen(bufp); check
  516. #define cat(s)    strncpy(bufp, s, buf+buflen-bufp-1); advance
  517. #else
  518. #define check
  519. #define ccat(c)    obstack_1grow (&scratch_obstack, (c));
  520. #define advance
  521. #define cat(s)    obstack_grow (&scratch_obstack, (s), strlen (s))
  522. #endif
  523.  
  524.   cat (name);
  525.   ccat ('<');
  526.   nparms = TREE_VEC_LENGTH (parms);
  527.   my_friendly_assert (nparms == TREE_VEC_LENGTH (arglist), 268);
  528.   for (i = 0; i < nparms; i++)
  529.     {
  530.       tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
  531.       tree arg = TREE_VEC_ELT (arglist, i);
  532.  
  533.       if (i)
  534.     ccat (',');
  535.  
  536.       if (TREE_CODE (parm) == TYPE_DECL)
  537.     {
  538.       cat (type_as_string (arg, 0));
  539.       continue;
  540.     }
  541.       else
  542.     my_friendly_assert (TREE_CODE (parm) == PARM_DECL, 269);
  543.  
  544.       if (TREE_CODE (arg) == TREE_LIST)
  545.     {
  546.       /* New list cell was built because old chain link was in
  547.          use.  */
  548.       my_friendly_assert (TREE_PURPOSE (arg) == NULL_TREE, 270);
  549.       arg = TREE_VALUE (arg);
  550.     }
  551.       /* No need to check arglist against parmlist here; we did that
  552.      in coerce_template_parms, called from lookup_template_class.  */
  553.       cat (expr_as_string (arg, 0));
  554.     }
  555.   {
  556.     char *bufp = obstack_next_free (&scratch_obstack);
  557.     int offset = 0;
  558.     while (bufp[offset - 1] == ' ')
  559.       offset--;
  560.     obstack_blank_fast (&scratch_obstack, offset);
  561.  
  562.     /* B<C<char> >, not B<C<char>> */
  563.     if (bufp[offset - 1] == '>')
  564.       ccat (' ');
  565.   }
  566.   ccat ('>');
  567.   ccat ('\0');
  568.   return (char *) obstack_base (&scratch_obstack);
  569.  
  570. #if 0
  571.  too_long:
  572. #endif
  573.   fatal ("out of (preallocated) string space creating template instantiation name");
  574.   /* NOTREACHED */
  575.   return NULL;
  576. }
  577.  
  578. /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
  579.    parameters, find the desired type.
  580.  
  581.    D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
  582.    Since ARGLIST is build on the decl_obstack, we must copy it here
  583.    to keep it from being reclaimed when the decl storage is reclaimed.
  584.  
  585.    IN_DECL, if non-NULL, is the template declaration we are trying to
  586.    instantiate.  */
  587. tree
  588. lookup_template_class (d1, arglist, in_decl)
  589.      tree d1, arglist;
  590.      tree in_decl;
  591. {
  592.   tree template, parmlist;
  593.   char *mangled_name;
  594.   tree id;
  595.  
  596.   my_friendly_assert (TREE_CODE (d1) == IDENTIFIER_NODE, 272);
  597.   template = IDENTIFIER_GLOBAL_VALUE (d1); /* XXX */
  598.   if (! template)
  599.     template = IDENTIFIER_CLASS_VALUE (d1);
  600.   /* With something like `template <class T> class X class X { ... };'
  601.      we could end up with D1 having nothing but an IDENTIFIER_LOCAL_VALUE.
  602.      We don't want to do that, but we have to deal with the situation, so
  603.      let's give them some syntax errors to chew on instead of a crash.  */
  604.   if (! template)
  605.     return error_mark_node;
  606.   if (TREE_CODE (template) != TEMPLATE_DECL)
  607.     {
  608.       cp_error ("non-template type `%T' used as a template", d1);
  609.       if (in_decl)
  610.     cp_error_at ("for template declaration `%D'", in_decl);
  611.       return error_mark_node;
  612.     }
  613.   parmlist = DECL_TEMPLATE_PARMS (template);
  614.  
  615.   arglist = coerce_template_parms (parmlist, arglist, template);
  616.   if (arglist == error_mark_node)
  617.     return error_mark_node;
  618.   if (uses_template_parms (arglist))
  619.     {
  620.       tree t = make_lang_type (UNINSTANTIATED_P_TYPE);
  621.       tree d;
  622.       id = make_anon_name ();
  623.       d = build_decl (TYPE_DECL, id, t);
  624.       TYPE_NAME (t) = d;
  625.       TYPE_VALUES (t) = build_tree_list (template, arglist);
  626.       pushdecl_top_level (d);
  627.     }
  628.   else
  629.     {
  630.       mangled_name = mangle_class_name_for_template (IDENTIFIER_POINTER (d1),
  631.                              parmlist, arglist);
  632.       id = get_identifier (mangled_name);
  633.     }
  634.   if (!IDENTIFIER_TEMPLATE (id))
  635.     {
  636.       arglist = copy_to_permanent (arglist);
  637.       IDENTIFIER_TEMPLATE (id) = perm_tree_cons (template, arglist, NULL_TREE);
  638.     }
  639.   return id;
  640. }
  641.  
  642. void
  643. push_template_decls (parmlist, arglist, class_level)
  644.      tree parmlist, arglist;
  645.      int class_level;
  646. {
  647.   int i, nparms;
  648.  
  649.   /* Don't want to push values into global context.  */
  650.   if (!class_level)
  651.     {
  652.       pushlevel (1);
  653.       declare_pseudo_global_level ();
  654.     }
  655.  
  656.   nparms = TREE_VEC_LENGTH (parmlist);
  657.  
  658.   for (i = 0; i < nparms; i++)
  659.     {
  660.       int requires_type, is_type;
  661.       tree parm = TREE_VALUE (TREE_VEC_ELT (parmlist, i));
  662.       tree arg = TREE_VEC_ELT (arglist, i);
  663.       tree decl = 0;
  664.  
  665.       requires_type = TREE_CODE (parm) == TYPE_DECL;
  666.       is_type = TREE_CODE_CLASS (TREE_CODE (arg)) == 't';
  667.       if (is_type)
  668.     {
  669.       /* add typename to namespace */
  670.       if (!requires_type)
  671.         {
  672.           error ("template use error: type provided where value needed");
  673.           continue;
  674.         }
  675.       decl = arg;
  676.       my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (decl)) == 't', 273);
  677.       decl = build_decl (TYPE_DECL, DECL_NAME (parm), decl);
  678.     }
  679.       else
  680.     {
  681.       /* add const decl to namespace */
  682.       tree val;
  683.       if (requires_type)
  684.         {
  685.           error ("template use error: value provided where type needed");
  686.           continue;
  687.         }
  688.       val = digest_init (TREE_TYPE (parm), arg, (tree *) 0);
  689.       if (val != error_mark_node)
  690.         {
  691.           decl = build_decl (CONST_DECL, DECL_NAME (parm),
  692.                  TREE_TYPE (parm));
  693.           DECL_INITIAL (decl) = val;
  694.           TREE_READONLY (decl) = 1;
  695.         }
  696.     }
  697.       if (decl != 0)
  698.     {
  699.       SET_DECL_ARTIFICIAL (decl);
  700.       layout_decl (decl, 0);
  701.       if (class_level)
  702.         pushdecl_class_level (decl);
  703.       else
  704.         pushdecl (decl);
  705.     }
  706.     }
  707. }
  708.  
  709. void
  710. pop_template_decls (parmlist, arglist, class_level)
  711.      tree parmlist, arglist;
  712.      int class_level;
  713. {
  714.   if (!class_level)
  715.     poplevel (0, 0, 0);
  716. }
  717.  
  718. /* Should be defined in parse.h.  */
  719. extern int yychar;
  720.  
  721. int
  722. uses_template_parms (t)
  723.      tree t;
  724. {
  725.   if (!t)
  726.     return 0;
  727.   switch (TREE_CODE (t))
  728.     {
  729.     case INDIRECT_REF:
  730.     case COMPONENT_REF:
  731.       /* We assume that the object must be instantiated in order to build
  732.      the COMPONENT_REF, so we test only whether the type of the
  733.      COMPONENT_REF uses template parms.  */
  734.       return uses_template_parms (TREE_TYPE (t));
  735.  
  736.     case IDENTIFIER_NODE:
  737.       if (!IDENTIFIER_TEMPLATE (t))
  738.     return 0;
  739.       return uses_template_parms (TREE_VALUE (IDENTIFIER_TEMPLATE (t)));
  740.  
  741.       /* aggregates of tree nodes */
  742.     case TREE_VEC:
  743.       {
  744.     int i = TREE_VEC_LENGTH (t);
  745.     while (i--)
  746.       if (uses_template_parms (TREE_VEC_ELT (t, i)))
  747.         return 1;
  748.     return 0;
  749.       }
  750.     case TREE_LIST:
  751.       if (uses_template_parms (TREE_PURPOSE (t))
  752.       || uses_template_parms (TREE_VALUE (t)))
  753.     return 1;
  754.       return uses_template_parms (TREE_CHAIN (t));
  755.  
  756.       /* constructed type nodes */
  757.     case POINTER_TYPE:
  758.     case REFERENCE_TYPE:
  759.       return uses_template_parms (TREE_TYPE (t));
  760.     case RECORD_TYPE:
  761.       if (TYPE_PTRMEMFUNC_FLAG (t))
  762.     return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (t));
  763.     case UNION_TYPE:
  764.       if (!TYPE_NAME (t))
  765.     return 0;
  766.       if (!TYPE_IDENTIFIER (t))
  767.     return 0;
  768.       return uses_template_parms (TYPE_IDENTIFIER (t));
  769.     case FUNCTION_TYPE:
  770.       if (uses_template_parms (TYPE_ARG_TYPES (t)))
  771.     return 1;
  772.       return uses_template_parms (TREE_TYPE (t));
  773.     case ARRAY_TYPE:
  774.       if (uses_template_parms (TYPE_DOMAIN (t)))
  775.     return 1;
  776.       return uses_template_parms (TREE_TYPE (t));
  777.     case OFFSET_TYPE:
  778.       if (uses_template_parms (TYPE_OFFSET_BASETYPE (t)))
  779.     return 1;
  780.       return uses_template_parms (TREE_TYPE (t));
  781.     case METHOD_TYPE:
  782.       if (uses_template_parms (TYPE_OFFSET_BASETYPE (t)))
  783.     return 1;
  784.       if (uses_template_parms (TYPE_ARG_TYPES (t)))
  785.     return 1;
  786.       return uses_template_parms (TREE_TYPE (t));
  787.  
  788.       /* decl nodes */
  789.     case TYPE_DECL:
  790.       return uses_template_parms (DECL_NAME (t));
  791.     case FUNCTION_DECL:
  792.       if (uses_template_parms (TREE_TYPE (t)))
  793.     return 1;
  794.       /* fall through */
  795.     case VAR_DECL:
  796.     case PARM_DECL:
  797.       /* ??? What about FIELD_DECLs?  */
  798.       /* The type of a decl can't use template parms if the name of the
  799.      variable doesn't, because it's impossible to resolve them.  So
  800.      ignore the type field for now.     */
  801.       if (DECL_CONTEXT (t) && uses_template_parms (DECL_CONTEXT (t)))
  802.     return 1;
  803.       if (uses_template_parms (TREE_TYPE (t)))
  804.     {
  805.       error ("template parms used where they can't be resolved");
  806.     }
  807.       return 0;
  808.  
  809.     case CALL_EXPR:
  810.       return uses_template_parms (TREE_TYPE (t));
  811.     case ADDR_EXPR:
  812.       return uses_template_parms (TREE_OPERAND (t, 0));
  813.  
  814.       /* template parm nodes */
  815.     case TEMPLATE_TYPE_PARM:
  816.     case TEMPLATE_CONST_PARM:
  817.       return 1;
  818.  
  819.       /* simple type nodes */
  820.     case INTEGER_TYPE:
  821.       if (uses_template_parms (TYPE_MIN_VALUE (t)))
  822.     return 1;
  823.       return uses_template_parms (TYPE_MAX_VALUE (t));
  824.  
  825.     case REAL_TYPE:
  826.     case VOID_TYPE:
  827.     case ENUMERAL_TYPE:
  828.     case BOOLEAN_TYPE:
  829.       return 0;
  830.  
  831.       /* constants */
  832.     case INTEGER_CST:
  833.     case REAL_CST:
  834.     case STRING_CST:
  835.       return 0;
  836.  
  837.     case ERROR_MARK:
  838.       /* Non-error_mark_node ERROR_MARKs are bad things.  */
  839.       my_friendly_assert (t == error_mark_node, 274);
  840.       /* NOTREACHED */
  841.       return 0;
  842.  
  843.     case UNINSTANTIATED_P_TYPE:
  844.       return 1;
  845.  
  846.     case CONSTRUCTOR:
  847.       if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
  848.     return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
  849.       /* else fall through */
  850.  
  851.     default:
  852.       switch (TREE_CODE_CLASS (TREE_CODE (t)))
  853.     {
  854.     case '1':
  855.     case '2':
  856.     case '3':
  857.     case '<':
  858.       {
  859.         int i;
  860.         for (i = tree_code_length[(int) TREE_CODE (t)]; --i >= 0;)
  861.           if (uses_template_parms (TREE_OPERAND (t, i)))
  862.         return 1;
  863.         return 0;
  864.       }
  865.     default:
  866.       break;
  867.     }
  868.       sorry ("testing %s for template parms",
  869.          tree_code_name [(int) TREE_CODE (t)]);
  870.       my_friendly_abort (82);
  871.       /* NOTREACHED */
  872.       return 0;
  873.     }
  874. }
  875.  
  876. void
  877. instantiate_member_templates (classname)
  878.      tree classname;
  879. {
  880.   tree t;
  881.   tree id = classname;
  882.   tree members = DECL_TEMPLATE_MEMBERS (TREE_PURPOSE (IDENTIFIER_TEMPLATE (id)));
  883.  
  884.   for (t = members; t; t = TREE_CHAIN (t))
  885.     {
  886.       tree parmvec, type, classparms, tdecl, t2;
  887.       int nparms, xxx = 0, i;
  888.  
  889.       my_friendly_assert (TREE_VALUE (t) != NULL_TREE, 275);
  890.       my_friendly_assert (TREE_CODE (TREE_VALUE (t)) == TEMPLATE_DECL, 276);
  891.       /* @@ Should verify that class parm list is a list of
  892.      distinct template parameters, and covers all the template
  893.      parameters.  */
  894.       tdecl = TREE_VALUE (t);
  895.       type = DECL_CONTEXT (DECL_TEMPLATE_RESULT (tdecl));
  896.       classparms = UPT_PARMS (type);
  897.       nparms = TREE_VEC_LENGTH (classparms);
  898.       parmvec = make_tree_vec (nparms);
  899.       for (i = 0; i < nparms; i++)
  900.     TREE_VEC_ELT (parmvec, i) = NULL_TREE;
  901.       switch (unify (DECL_TEMPLATE_PARMS (tdecl),
  902.              &TREE_VEC_ELT (parmvec, 0), nparms,
  903.              type, IDENTIFIER_TYPE_VALUE (classname),
  904.              &xxx))
  905.     {
  906.     case 0:
  907.       /* Success -- well, no inconsistency, at least.  */
  908.       for (i = 0; i < nparms; i++)
  909.         if (TREE_VEC_ELT (parmvec, i) == NULL_TREE)
  910.           goto failure;
  911.       t2 = instantiate_template (tdecl,
  912.                      &TREE_VEC_ELT (parmvec, 0));
  913.       type = IDENTIFIER_TYPE_VALUE (id);
  914.       my_friendly_assert (type != 0, 277);
  915.       break;
  916.     case 1:
  917.       /* Failure.  */
  918.     failure:
  919.       cp_error_at ("type unification error instantiating `%D'", tdecl);
  920.       cp_error ("while instantiating members of `%T'", classname);
  921.  
  922.       continue /* loop of members */;
  923.     default:
  924.       /* Eek, a bug.  */
  925.       my_friendly_abort (83);
  926.     }
  927.     }
  928. }
  929.  
  930. static struct tinst_level *current_tinst_level = 0;
  931. static struct tinst_level *free_tinst_level = 0;
  932. static int tinst_depth = 0;
  933. int max_tinst_depth = 17;
  934.  
  935. int
  936. push_tinst_level (name)
  937.      tree name;
  938. {
  939.   struct tinst_level *new;
  940.   tree global = IDENTIFIER_GLOBAL_VALUE (name);
  941.  
  942.   if (tinst_depth >= max_tinst_depth)
  943.     {
  944.       error ("template instantiation depth exceeds maximum of %d",
  945.          max_tinst_depth);
  946.       cp_error ("  instantiating `%D'", name);
  947.       return 0;
  948.     }
  949.  
  950.   if (free_tinst_level)
  951.     {
  952.       new = free_tinst_level;
  953.       free_tinst_level = new->next;
  954.     }
  955.   else
  956.     new = (struct tinst_level *) xmalloc (sizeof (struct tinst_level));
  957.  
  958.   new->classname = name;
  959.   if (global)
  960.     {
  961.       new->line = DECL_SOURCE_LINE (global);
  962.       new->file = DECL_SOURCE_FILE (global);
  963.     }
  964.   else
  965.     {
  966.       new->line = lineno;
  967.       new->file = input_filename;
  968.     }
  969.   new->next = current_tinst_level;
  970.   current_tinst_level = new;
  971.   ++tinst_depth;
  972.   return 1;
  973. }
  974.  
  975. void
  976. pop_tinst_level ()
  977. {
  978.   struct tinst_level *old = current_tinst_level;
  979.  
  980.   current_tinst_level = old->next;
  981.   old->next = free_tinst_level;
  982.   free_tinst_level = old;
  983.   --tinst_depth;
  984. }
  985.  
  986. struct tinst_level *
  987. tinst_for_decl ()
  988. {
  989.   struct tinst_level *p = current_tinst_level;
  990.  
  991.   if (p)
  992.     for (; p->next ; p = p->next )
  993.       ;
  994.   return p;
  995. }
  996.  
  997. tree
  998. instantiate_class_template (classname, setup_parse)
  999.      tree classname;
  1000.      int setup_parse;
  1001. {
  1002.   struct template_info *template_info;
  1003.   tree template, t1;
  1004.  
  1005.   if (classname == error_mark_node)
  1006.     return error_mark_node;
  1007.  
  1008.   my_friendly_assert (TREE_CODE (classname) == IDENTIFIER_NODE, 278);
  1009.   template = IDENTIFIER_TEMPLATE (classname);
  1010.  
  1011.   if (IDENTIFIER_HAS_TYPE_VALUE (classname))
  1012.     {
  1013.       tree type = IDENTIFIER_TYPE_VALUE (classname);
  1014.       if (TREE_CODE (type) == UNINSTANTIATED_P_TYPE)
  1015.     return type;
  1016.       if (TYPE_BEING_DEFINED (type)
  1017.       || TYPE_SIZE (type)
  1018.       || CLASSTYPE_USE_TEMPLATE (type) != 0)
  1019.     return type;
  1020.     }
  1021.  
  1022.   /* If IDENTIFIER_LOCAL_VALUE is already set on this template classname
  1023.      (it's something like `foo<int>'), that means we're already working on
  1024.      the instantiation for it.  Normally, a classname comes in with nothing
  1025.      but its IDENTIFIER_TEMPLATE slot set.  If we were to try to instantiate
  1026.      this again, we'd get a redeclaration error.  Since we're already working
  1027.      on it, we'll pass back this classname's TYPE_DECL (it's the value of
  1028.      the classname's IDENTIFIER_LOCAL_VALUE).  Only do this if we're setting
  1029.      things up for the parser, though---if we're just trying to instantiate
  1030.      it (e.g., via tsubst) we can trip up cuz it may not have an
  1031.      IDENTIFIER_TYPE_VALUE when it will need one.  */
  1032.   if (setup_parse && IDENTIFIER_LOCAL_VALUE (classname))
  1033.     return IDENTIFIER_LOCAL_VALUE (classname);
  1034.  
  1035.   if (uses_template_parms (classname))
  1036.     {
  1037.       if (!TREE_TYPE (classname))
  1038.     {
  1039.       tree t = make_lang_type (RECORD_TYPE);
  1040.       tree d = build_decl (TYPE_DECL, classname, t);
  1041.       DECL_NAME (d) = classname;
  1042.       TYPE_NAME (t) = d;
  1043.       pushdecl (d);
  1044.     }
  1045.       return NULL_TREE;
  1046.     }
  1047.  
  1048.   t1 = TREE_PURPOSE (template);
  1049.   my_friendly_assert (TREE_CODE (t1) == TEMPLATE_DECL, 279);
  1050.  
  1051.   /* If a template is declared but not defined, accept it; don't crash.
  1052.      Later uses requiring the definition will be flagged as errors by
  1053.      other code.  Thanks to niklas@appli.se for this bug fix.  */
  1054.   if (DECL_TEMPLATE_INFO (t1)->text == 0)
  1055.     setup_parse = 0;
  1056.  
  1057.   push_to_top_level ();
  1058.   template_info = DECL_TEMPLATE_INFO (t1);
  1059.   if (setup_parse && push_tinst_level (classname))
  1060.     {
  1061.       push_template_decls (DECL_TEMPLATE_PARMS (TREE_PURPOSE (template)),
  1062.                TREE_VALUE (template), 0);
  1063.       set_current_level_tags_transparency (1);
  1064.       feed_input (template_info->text, template_info->length, (struct obstack *)0);
  1065.       lineno = template_info->lineno;
  1066.       input_filename = template_info->filename;
  1067.       /* Get interface/implementation back in sync.  */
  1068.       extract_interface_info ();
  1069.       overload_template_name (classname, 0);
  1070.       /* Kludge so that we don't get screwed by our own base classes.  */
  1071.       TYPE_BEING_DEFINED (TREE_TYPE (classname)) = 1;
  1072.       yychar = PRE_PARSED_CLASS_DECL;
  1073.       yylval.ttype = classname;
  1074.       processing_template_defn++;
  1075.       if (!flag_external_templates)
  1076.     interface_unknown++;
  1077.     }
  1078.   else
  1079.     {
  1080.       tree t, decl, id, tmpl;
  1081.  
  1082.       id = classname;
  1083.       tmpl = TREE_PURPOSE (IDENTIFIER_TEMPLATE (id));
  1084.       t = xref_tag (DECL_TEMPLATE_INFO (tmpl)->aggr, id, NULL_TREE, 0);
  1085.       my_friendly_assert (TREE_CODE (t) == RECORD_TYPE
  1086.               || TREE_CODE (t) == UNION_TYPE, 280);
  1087.  
  1088.       /* Now, put a copy of the decl in global scope, to avoid
  1089.        * recursive expansion.  */
  1090.       decl = IDENTIFIER_LOCAL_VALUE (id);
  1091.       if (!decl)
  1092.     decl = IDENTIFIER_CLASS_VALUE (id);
  1093.       if (decl)
  1094.     {
  1095.       my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 281);
  1096.       /* We'd better make sure we're on the permanent obstack or else
  1097.        * we'll get a "friendly" abort 124 in pushdecl.  Perhaps a
  1098.        * copy_to_permanent would be sufficient here, but then a
  1099.        * sharing problem might occur.  I don't know -- niklas@appli.se */
  1100.       push_obstacks (&permanent_obstack, &permanent_obstack);
  1101.       pushdecl_top_level (copy_node (decl));
  1102.       pop_obstacks ();
  1103.     }
  1104.       pop_from_top_level ();
  1105.     }
  1106.  
  1107.   return NULL_TREE;
  1108. }
  1109.  
  1110. static int
  1111. list_eq (t1, t2)
  1112.      tree t1, t2;
  1113. {
  1114.   if (t1 == NULL_TREE)
  1115.     return t2 == NULL_TREE;
  1116.   if (t2 == NULL_TREE)
  1117.     return 0;
  1118.   /* Don't care if one declares its arg const and the other doesn't -- the
  1119.      main variant of the arg type is all that matters.  */
  1120.   if (TYPE_MAIN_VARIANT (TREE_VALUE (t1))
  1121.       != TYPE_MAIN_VARIANT (TREE_VALUE (t2)))
  1122.     return 0;
  1123.   return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2));
  1124. }
  1125.  
  1126. static tree 
  1127. lookup_nested_type_by_name (ctype, name)
  1128.         tree ctype, name;
  1129. {
  1130.   tree t;
  1131.  
  1132.   for (t = CLASSTYPE_TAGS (ctype); t; t = TREE_CHAIN (t))
  1133.     {
  1134.       if (name == TREE_PURPOSE (t))
  1135.     return TREE_VALUE (t);
  1136.     }
  1137.   return NULL_TREE;
  1138. }
  1139.  
  1140. static tree
  1141. search_nested_type_in_tmpl (tmpl, type)
  1142.         tree tmpl, type;
  1143. {
  1144.   tree t;
  1145.  
  1146.   if (tmpl == NULL || TYPE_CONTEXT(type) == NULL)
  1147.     return tmpl;
  1148.   t = search_nested_type_in_tmpl (tmpl, TYPE_CONTEXT(type));
  1149.   if (t == NULL) return t;
  1150.   t = lookup_nested_type_by_name(t, DECL_NAME(TYPE_NAME(type)));
  1151.   return t;
  1152. }
  1153.  
  1154. static tree
  1155. tsubst (t, args, nargs, in_decl)
  1156.      tree t, *args;
  1157.      int nargs;
  1158.      tree in_decl;
  1159. {
  1160.   tree type;
  1161.  
  1162.   if (t == NULL_TREE || t == error_mark_node)
  1163.     return t;
  1164.  
  1165.   type = TREE_TYPE (t);
  1166.   if (type
  1167.       /* Minor optimization.
  1168.      ?? Are these really the most frequent cases?  Is the savings
  1169.      significant?  */
  1170.       && type != integer_type_node
  1171.       && type != void_type_node
  1172.       && type != char_type_node)
  1173.     type = tsubst (type, args, nargs, in_decl);
  1174.  
  1175.   switch (TREE_CODE (t))
  1176.     {
  1177.     case RECORD_TYPE:
  1178.       if (TYPE_PTRMEMFUNC_P (t))
  1179.     return build_ptrmemfunc_type
  1180.       (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, nargs, in_decl));
  1181.       
  1182.       /* else fall through */
  1183.  
  1184.     case ERROR_MARK:
  1185.     case IDENTIFIER_NODE:
  1186.     case OP_IDENTIFIER:
  1187.     case VOID_TYPE:
  1188.     case REAL_TYPE:
  1189.     case ENUMERAL_TYPE:
  1190.     case BOOLEAN_TYPE:
  1191.     case INTEGER_CST:
  1192.     case REAL_CST:
  1193.     case STRING_CST:
  1194.     case UNION_TYPE:
  1195.       return t;
  1196.  
  1197.     case INTEGER_TYPE:
  1198.       if (t == integer_type_node)
  1199.     return t;
  1200.  
  1201.       if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
  1202.       && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
  1203.     return t;
  1204.       return build_index_2_type
  1205.     (tsubst (TYPE_MIN_VALUE (t), args, nargs, in_decl),
  1206.      tsubst (TYPE_MAX_VALUE (t), args, nargs, in_decl));
  1207.  
  1208.     case TEMPLATE_TYPE_PARM:
  1209.       {
  1210.     tree arg = args[TEMPLATE_TYPE_IDX (t)];
  1211.     return cp_build_type_variant
  1212.       (arg, TYPE_READONLY (arg) || TYPE_READONLY (t),
  1213.        TYPE_VOLATILE (arg) || TYPE_VOLATILE (t));
  1214.       }
  1215.  
  1216.     case TEMPLATE_CONST_PARM:
  1217.       return args[TEMPLATE_CONST_IDX (t)];
  1218.  
  1219.     case FUNCTION_DECL:
  1220.       {
  1221.     tree r;
  1222.     tree fnargs, result;
  1223.     
  1224.     if (type == TREE_TYPE (t)
  1225.         && (DECL_CONTEXT (t) == NULL_TREE
  1226.         || TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t))) != 't'))
  1227.       return t;
  1228.     fnargs = tsubst (DECL_ARGUMENTS (t), args, nargs, t);
  1229.     result = tsubst (DECL_RESULT (t), args, nargs, t);
  1230.     if (DECL_CONTEXT (t) != NULL_TREE
  1231.         && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t))) == 't')
  1232.       {
  1233.         /* Look it up in that class, and return the decl node there,
  1234.            instead of creating a new one.  */
  1235.         tree ctx, methods, name, method;
  1236.         int n_methods;
  1237.         int i, found = 0;
  1238.  
  1239.         name = DECL_NAME (t);
  1240.         ctx = tsubst (DECL_CONTEXT (t), args, nargs, t);
  1241.         methods = CLASSTYPE_METHOD_VEC (ctx);
  1242.         if (methods == NULL_TREE)
  1243.           /* No methods at all -- no way this one can match.  */
  1244.           goto no_match;
  1245.         n_methods = TREE_VEC_LENGTH (methods);
  1246.  
  1247.         r = NULL_TREE;
  1248.  
  1249.         if (!strncmp (OPERATOR_TYPENAME_FORMAT,
  1250.               IDENTIFIER_POINTER (name),
  1251.               sizeof (OPERATOR_TYPENAME_FORMAT) - 1))
  1252.           {
  1253.         /* Type-conversion operator.  Reconstruct the name, in
  1254.            case it's the name of one of the template's parameters.  */
  1255.         name = build_typename_overload (TREE_TYPE (type));
  1256.           }
  1257.  
  1258.         if (DECL_CONTEXT (t) != NULL_TREE
  1259.         && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t))) == 't'
  1260.         && constructor_name (DECL_CONTEXT (t)) == DECL_NAME (t))
  1261.           name = constructor_name (ctx);
  1262.  
  1263.         if (DECL_CONSTRUCTOR_P (t) && TYPE_USES_VIRTUAL_BASECLASSES (ctx))
  1264.           {
  1265.         /* Since we didn't know that this class had virtual bases until after
  1266.            we instantiated it, we have to recreate the arguments to this
  1267.            constructor, as otherwise it would miss the __in_chrg parameter.  */
  1268.         tree newtype, parm;
  1269.         tree parms = TREE_CHAIN (TYPE_ARG_TYPES (type));
  1270.         parms = hash_tree_chain (integer_type_node, parms);
  1271.         newtype = build_cplus_method_type (ctx,
  1272.                            TREE_TYPE (type),
  1273.                            parms);
  1274.         newtype = build_type_variant (newtype,
  1275.                           TYPE_READONLY (type),
  1276.                           TYPE_VOLATILE (type));
  1277.         type = newtype;
  1278.  
  1279.         fnargs = copy_node (DECL_ARGUMENTS (t));
  1280.         TREE_CHAIN (fnargs) = TREE_CHAIN (DECL_ARGUMENTS (t));
  1281.  
  1282.         /* In this case we need "in-charge" flag saying whether
  1283.            this constructor is responsible for initialization
  1284.            of virtual baseclasses or not.  */
  1285.         parm = build_decl (PARM_DECL, in_charge_identifier, integer_type_node);
  1286.         /* Mark the artificial `__in_chrg' parameter as "artificial".  */
  1287.         SET_DECL_ARTIFICIAL (parm);
  1288.         DECL_ARG_TYPE (parm) = integer_type_node;
  1289.         DECL_REGISTER (parm) = 1;
  1290.         TREE_CHAIN (parm) = TREE_CHAIN (fnargs);
  1291.         TREE_CHAIN (fnargs) = parm;
  1292.  
  1293.         fnargs = tsubst (fnargs, args, nargs, t);
  1294.           }
  1295. #if 0
  1296.         fprintf (stderr, "\nfor function %s in class %s:\n",
  1297.              IDENTIFIER_POINTER (name),
  1298.              IDENTIFIER_POINTER (TYPE_IDENTIFIER (ctx)));
  1299. #endif
  1300.         for (i = 0; i < n_methods; i++)
  1301.           {
  1302.         int pass;
  1303.  
  1304.         method = TREE_VEC_ELT (methods, i);
  1305.         if (method == NULL_TREE || DECL_NAME (method) != name)
  1306.           continue;
  1307.  
  1308.         pass = 0;
  1309.           maybe_error:
  1310.         for (; method; method = DECL_CHAIN (method))
  1311.           {
  1312.             my_friendly_assert (TREE_CODE (method) == FUNCTION_DECL,
  1313.                     282);
  1314.             if (! comptypes (type, TREE_TYPE (method), 1))
  1315.               {
  1316.             tree mtype = TREE_TYPE (method);
  1317.             tree t1, t2;
  1318.  
  1319.             /* Keep looking for a method that matches
  1320.                perfectly.  This takes care of the problem
  1321.                where destructors (which have implicit int args)
  1322.                look like constructors which have an int arg.  */
  1323.             if (pass == 0)
  1324.               continue;
  1325.  
  1326.             t1 = TYPE_ARG_TYPES (mtype);
  1327.             t2 = TYPE_ARG_TYPES (type);
  1328.             if (TREE_CODE (mtype) == FUNCTION_TYPE)
  1329.               t2 = TREE_CHAIN (t2);
  1330.  
  1331.             if (list_eq (t1, t2))
  1332.               {
  1333.                 if (TREE_CODE (mtype) == FUNCTION_TYPE)
  1334.                   {
  1335.                 tree newtype;
  1336.                 newtype = build_function_type (TREE_TYPE (type),
  1337.                                    TYPE_ARG_TYPES (type));
  1338.                 newtype = build_type_variant (newtype,
  1339.                                   TYPE_READONLY (type),
  1340.                                   TYPE_VOLATILE (type));
  1341.                 type = newtype;
  1342.                 if (TREE_TYPE (type) != TREE_TYPE (mtype))
  1343.                   goto maybe_bad_return_type;
  1344.                   }
  1345.                 else if (TYPE_METHOD_BASETYPE (mtype)
  1346.                      == TYPE_METHOD_BASETYPE (type))
  1347.                   {
  1348.                 /* Types didn't match, but arg types and
  1349.                    `this' do match, so the return type is
  1350.                    all that should be messing it up.  */
  1351.                   maybe_bad_return_type:
  1352.                 if (TREE_TYPE (type) != TREE_TYPE (mtype))
  1353.                   error ("inconsistent return types for method `%s' in class `%s'",
  1354.                      IDENTIFIER_POINTER (name),
  1355.                      IDENTIFIER_POINTER (TYPE_IDENTIFIER (ctx)));
  1356.                   }
  1357.                 r = method;
  1358.                 break;
  1359.               }
  1360.             found = 1;
  1361.             continue;
  1362.               }
  1363. #if 0
  1364.             fprintf (stderr, "\tfound %s\n\n",
  1365.                  IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (method)));
  1366. #endif
  1367.             if (DECL_ARTIFICIAL (method))
  1368.               {
  1369.             cp_error ("template for method `%D' which has default implementation in class `%T'", name, ctx);
  1370.             if (in_decl)
  1371.               cp_error_at ("in attempt to instantiate `%D' declared at this point in file", in_decl);
  1372.             return error_mark_node;
  1373.               }
  1374.  
  1375.             if (DECL_ARGUMENTS (method)
  1376.             && ! TREE_PERMANENT (DECL_ARGUMENTS (method)))
  1377.               /* @@ Is this early enough?  Might we want to do
  1378.              this instead while processing the expansion?     */
  1379.               DECL_ARGUMENTS (method)
  1380.             = tsubst (DECL_ARGUMENTS (t), args, nargs, t);
  1381.             r = method;
  1382.             break;
  1383.           }
  1384.         if (r == NULL_TREE && pass == 0)
  1385.           {
  1386.             pass = 1;
  1387.             method = TREE_VEC_ELT (methods, i);
  1388.             goto maybe_error;
  1389.           }
  1390.           }
  1391.         if (r == NULL_TREE)
  1392.           {
  1393.           no_match:
  1394.         cp_error
  1395.           (found
  1396.            ? "template for method `%D' doesn't match any in class `%T'"
  1397.            : "method `%D' not found in class `%T'", name, ctx);
  1398.         if (in_decl)
  1399.           cp_error_at ("in attempt to instantiate `%D' declared at this point in file", in_decl);
  1400.         return error_mark_node;
  1401.           }
  1402.       }
  1403.     else
  1404.       {
  1405.         r = DECL_NAME (t);
  1406.         {
  1407.           tree decls;
  1408.           int got_it = 0;
  1409.  
  1410.           decls = lookup_name_nonclass (r);
  1411.           if (decls == NULL_TREE)
  1412.         /* no match */;
  1413.           else if (TREE_CODE (decls) == TREE_LIST)
  1414.         for (decls = TREE_VALUE (decls); decls ;
  1415.              decls = DECL_CHAIN (decls))
  1416.           {
  1417.             if (TREE_CODE (decls) == FUNCTION_DECL
  1418.             && TREE_TYPE (decls) == type)
  1419.               {
  1420.             got_it = 1;
  1421.             r = decls;
  1422.             break;
  1423.               }
  1424.           }
  1425.           else
  1426.         {
  1427.           tree val = decls;
  1428.           decls = NULL_TREE;
  1429.           if (TREE_CODE (val) == FUNCTION_DECL
  1430.               && TREE_TYPE (val) == type)
  1431.             {
  1432.               got_it = 1;
  1433.               r = val;
  1434.             }
  1435.         }
  1436.  
  1437.           if (!got_it)
  1438.         {
  1439.           tree a = build_decl_overload (r, TYPE_VALUES (type),
  1440.                         DECL_CONTEXT (t) != NULL_TREE);
  1441.           r = build_lang_decl (FUNCTION_DECL, r, type);
  1442.           DECL_ASSEMBLER_NAME (r) = a;
  1443.         }
  1444.           else if (TREE_STATIC (r))
  1445.         {
  1446.           /* This overrides the template version, use it. */
  1447.           return r;
  1448.         }
  1449.         }
  1450.       }
  1451.     TREE_PUBLIC (r) = 1;
  1452.     DECL_EXTERNAL (r) = 1;
  1453.     TREE_STATIC (r) = 0;
  1454.     DECL_INTERFACE_KNOWN (r) = 0;
  1455.     DECL_INLINE (r) = DECL_INLINE (t);
  1456.     DECL_THIS_INLINE (r) = DECL_THIS_INLINE (t);
  1457.     TREE_READONLY (r) = TREE_READONLY (t);
  1458.     TREE_THIS_VOLATILE (r) = TREE_THIS_VOLATILE (t);
  1459.     {
  1460. #if 0                /* Maybe later.  -jason  */
  1461.       struct tinst_level *til = tinst_for_decl();
  1462.  
  1463.       /* should always be true under new approach */
  1464.       if (til)
  1465.         {
  1466.           DECL_SOURCE_FILE (r) = til->file;
  1467.           DECL_SOURCE_LINE (r) = til->line;
  1468.         }
  1469.       else
  1470. #endif
  1471.         {
  1472.           DECL_SOURCE_FILE (r) = DECL_SOURCE_FILE (t);
  1473.           DECL_SOURCE_LINE (r) = DECL_SOURCE_LINE (t);
  1474.         }
  1475.     }
  1476.     DECL_CLASS_CONTEXT (r) = tsubst (DECL_CLASS_CONTEXT (t), args, nargs, t);
  1477.     make_decl_rtl (r, NULL_PTR, 1);
  1478.     DECL_ARGUMENTS (r) = fnargs;
  1479.     DECL_RESULT (r) = result;
  1480. #if 0
  1481.     if (DECL_CONTEXT (t) == NULL_TREE
  1482.         || TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t))) != 't')
  1483.       push_overloaded_decl_top_level (r, 0);
  1484. #endif
  1485.     return r;
  1486.       }
  1487.  
  1488.     case PARM_DECL:
  1489.       {
  1490.     tree r;
  1491.     r = build_decl (PARM_DECL, DECL_NAME (t), type);
  1492.     DECL_INITIAL (r) = TREE_TYPE (r);
  1493.     if (TREE_CHAIN (t))
  1494.       TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args, nargs, TREE_CHAIN (t));
  1495.     return r;
  1496.       }
  1497.  
  1498.     case TREE_LIST:
  1499.       {
  1500.     tree purpose, value, chain, result;
  1501.     int via_public, via_virtual, via_protected;
  1502.  
  1503.     if (t == void_list_node)
  1504.       return t;
  1505.  
  1506.     via_public = TREE_VIA_PUBLIC (t);
  1507.     via_protected = TREE_VIA_PROTECTED (t);
  1508.     via_virtual = TREE_VIA_VIRTUAL (t);
  1509.  
  1510.     purpose = TREE_PURPOSE (t);
  1511.     if (purpose)
  1512.       purpose = tsubst (purpose, args, nargs, in_decl);
  1513.     value = TREE_VALUE (t);
  1514.     if (value)
  1515.       value = tsubst (value, args, nargs, in_decl);
  1516.     chain = TREE_CHAIN (t);
  1517.     if (chain && chain != void_type_node)
  1518.       chain = tsubst (chain, args, nargs, in_decl);
  1519.     if (purpose == TREE_PURPOSE (t)
  1520.         && value == TREE_VALUE (t)
  1521.         && chain == TREE_CHAIN (t))
  1522.       return t;
  1523.     result = hash_tree_cons (via_public, via_virtual, via_protected,
  1524.                  purpose, value, chain);
  1525.     TREE_PARMLIST (result) = TREE_PARMLIST (t);
  1526.     return result;
  1527.       }
  1528.     case TREE_VEC:
  1529.       {
  1530.     int len = TREE_VEC_LENGTH (t), need_new = 0, i;
  1531.     tree *elts = (tree *) alloca (len * sizeof (tree));
  1532.     bzero ((char *) elts, len * sizeof (tree));
  1533.  
  1534.     for (i = 0; i < len; i++)
  1535.       {
  1536.         elts[i] = tsubst (TREE_VEC_ELT (t, i), args, nargs, in_decl);
  1537.         if (elts[i] != TREE_VEC_ELT (t, i))
  1538.           need_new = 1;
  1539.       }
  1540.  
  1541.     if (!need_new)
  1542.       return t;
  1543.  
  1544.     t = make_tree_vec (len);
  1545.     for (i = 0; i < len; i++)
  1546.       TREE_VEC_ELT (t, i) = elts[i];
  1547.     return t;
  1548.       }
  1549.     case POINTER_TYPE:
  1550.     case REFERENCE_TYPE:
  1551.       {
  1552.     tree r;
  1553.     enum tree_code code;
  1554.     if (type == TREE_TYPE (t))
  1555.       return t;
  1556.  
  1557.     code = TREE_CODE (t);
  1558.     if (code == POINTER_TYPE)
  1559.       r = build_pointer_type (type);
  1560.     else
  1561.       r = build_reference_type (type);
  1562.     r = cp_build_type_variant (r, TYPE_READONLY (t), TYPE_VOLATILE (t));
  1563.     /* Will this ever be needed for TYPE_..._TO values?  */
  1564.     layout_type (r);
  1565.     return r;
  1566.       }
  1567.     case OFFSET_TYPE:
  1568.       return build_offset_type
  1569.     (tsubst (TYPE_OFFSET_BASETYPE (t), args, nargs, in_decl), type);
  1570.     case FUNCTION_TYPE:
  1571.     case METHOD_TYPE:
  1572.       {
  1573.     tree values = TYPE_VALUES (t); /* same as TYPE_ARG_TYPES */
  1574.     tree context = TYPE_CONTEXT (t);
  1575.     tree new_value;
  1576.  
  1577.     /* Don't bother recursing if we know it won't change anything.    */
  1578.     if (values != void_list_node)
  1579.       values = tsubst (values, args, nargs, in_decl);
  1580.     if (context)
  1581.       context = tsubst (context, args, nargs, in_decl);
  1582.     /* Could also optimize cases where return value and
  1583.        values have common elements (e.g., T min(const &T, const T&).  */
  1584.  
  1585.     /* If the above parameters haven't changed, just return the type.  */
  1586.     if (type == TREE_TYPE (t)
  1587.         && values == TYPE_VALUES (t)
  1588.         && context == TYPE_CONTEXT (t))
  1589.       return t;
  1590.  
  1591.     /* Construct a new type node and return it.  */
  1592.     if (TREE_CODE (t) == FUNCTION_TYPE
  1593.         && context == NULL_TREE)
  1594.       {
  1595.         new_value = build_function_type (type, values);
  1596.       }
  1597.     else if (context == NULL_TREE)
  1598.       {
  1599.         tree base = tsubst (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t))),
  1600.                 args, nargs, in_decl);
  1601.         new_value = build_cplus_method_type (base, type,
  1602.                          TREE_CHAIN (values));
  1603.       }
  1604.     else
  1605.       {
  1606.         new_value = make_node (TREE_CODE (t));
  1607.         TREE_TYPE (new_value) = type;
  1608.         TYPE_CONTEXT (new_value) = context;
  1609.         TYPE_VALUES (new_value) = values;
  1610.         TYPE_SIZE (new_value) = TYPE_SIZE (t);
  1611.         TYPE_ALIGN (new_value) = TYPE_ALIGN (t);
  1612.         TYPE_MODE (new_value) = TYPE_MODE (t);
  1613.         if (TYPE_METHOD_BASETYPE (t))
  1614.           TYPE_METHOD_BASETYPE (new_value) = tsubst (TYPE_METHOD_BASETYPE (t),
  1615.                              args, nargs, in_decl);
  1616.         /* Need to generate hash value.  */
  1617.         my_friendly_abort (84);
  1618.       }
  1619.     new_value = build_type_variant (new_value,
  1620.                     TYPE_READONLY (t),
  1621.                     TYPE_VOLATILE (t));
  1622.     return new_value;
  1623.       }
  1624.     case ARRAY_TYPE:
  1625.       {
  1626.     tree domain = tsubst (TYPE_DOMAIN (t), args, nargs, in_decl);
  1627.     tree r;
  1628.     if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
  1629.       return t;
  1630.     r = build_cplus_array_type (type, domain);
  1631.     return r;
  1632.       }
  1633.  
  1634.     case UNINSTANTIATED_P_TYPE:
  1635.       {
  1636.     int nparms = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (UPT_TEMPLATE (t)));
  1637.     tree argvec = make_tree_vec (nparms);
  1638.     tree parmvec = UPT_PARMS (t);
  1639.     int i;
  1640.     tree id, rt;
  1641.     for (i = 0; i < nparms; i++)
  1642.       TREE_VEC_ELT (argvec, i) = tsubst (TREE_VEC_ELT (parmvec, i),
  1643.                          args, nargs, in_decl);
  1644.     id = lookup_template_class (DECL_NAME (UPT_TEMPLATE (t)), argvec, NULL_TREE);
  1645.     if (! IDENTIFIER_HAS_TYPE_VALUE (id)) {
  1646.       instantiate_class_template(id, 0);
  1647.       /* set up pending_classes */
  1648.       add_pending_template (id);
  1649.  
  1650.       TYPE_MAIN_VARIANT (IDENTIFIER_TYPE_VALUE (id)) =
  1651.         IDENTIFIER_TYPE_VALUE (id);
  1652.     }
  1653.         rt = IDENTIFIER_TYPE_VALUE (id);
  1654.  
  1655.     /* kung: this part handles nested type in template definition */
  1656.         
  1657.     if ( !ANON_AGGRNAME_P (DECL_NAME(TYPE_NAME(t))))
  1658.           {
  1659.         rt = search_nested_type_in_tmpl (rt, t);
  1660.           }
  1661.  
  1662.     return build_type_variant (rt, TYPE_READONLY (t), TYPE_VOLATILE (t));
  1663.       }
  1664.  
  1665.     case MINUS_EXPR:
  1666.     case PLUS_EXPR:
  1667.       return fold (build (TREE_CODE (t), TREE_TYPE (t),
  1668.               tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
  1669.               tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl)));
  1670.  
  1671.     case NEGATE_EXPR:
  1672.     case NOP_EXPR:
  1673.       return fold (build1 (TREE_CODE (t), TREE_TYPE (t),
  1674.                tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl)));
  1675.  
  1676.     default:
  1677.       sorry ("use of `%s' in function template",
  1678.          tree_code_name [(int) TREE_CODE (t)]);
  1679.       return error_mark_node;
  1680.     }
  1681. }
  1682.  
  1683. tree
  1684. instantiate_template (tmpl, targ_ptr)
  1685.      tree tmpl, *targ_ptr;
  1686. {
  1687.   tree targs, fndecl;
  1688.   int i, len;
  1689.   struct pending_inline *p;
  1690.   struct template_info *t;
  1691.   struct obstack *old_fmp_obstack;
  1692.   extern struct obstack *function_maybepermanent_obstack;
  1693.  
  1694.   push_obstacks (&permanent_obstack, &permanent_obstack);
  1695.   old_fmp_obstack = function_maybepermanent_obstack;
  1696.   function_maybepermanent_obstack = &permanent_obstack;
  1697.  
  1698.   my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283);
  1699.   len = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (tmpl));
  1700.  
  1701.   i = len;
  1702.   while (i--)
  1703.     targ_ptr[i] = copy_to_permanent (targ_ptr[i]);
  1704.  
  1705.   for (fndecl = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
  1706.        fndecl; fndecl = TREE_CHAIN (fndecl))
  1707.     {
  1708.       tree *t1 = &TREE_VEC_ELT (TREE_PURPOSE (fndecl), 0);
  1709.       for (i = len - 1; i >= 0; i--)
  1710.     if (simple_cst_equal (t1[i], targ_ptr[i]) <= 0)
  1711.       goto no_match;
  1712.  
  1713.       /* Here, we have a match.  */
  1714.       fndecl = TREE_VALUE (fndecl);
  1715.       goto exit;
  1716.  
  1717.     no_match:
  1718.       ;
  1719.     }
  1720.  
  1721.   targs = make_tree_vec (len);
  1722.   i = len;
  1723.   while (i--)
  1724.     TREE_VEC_ELT (targs, i) = targ_ptr[i];
  1725.  
  1726.   /* substitute template parameters */
  1727.   fndecl = tsubst (DECL_RESULT (tmpl), targ_ptr,
  1728.            TREE_VEC_LENGTH (targs), tmpl);
  1729.  
  1730.   if (fndecl == error_mark_node)
  1731.     goto exit;
  1732.  
  1733.   assemble_external (fndecl);
  1734.  
  1735.   /* If it's a static member fn in the template, we need to change it
  1736.      into a FUNCTION_TYPE and chop off its this pointer.  */
  1737.   if (TREE_CODE (TREE_TYPE (DECL_RESULT (tmpl))) == METHOD_TYPE
  1738.       && DECL_STATIC_FUNCTION_P (fndecl))
  1739.     {
  1740.       tree olddecl = DECL_RESULT (tmpl);
  1741.       revert_static_member_fn (&DECL_RESULT (tmpl), NULL, NULL);
  1742.       /* Chop off the this pointer that grokclassfn so kindly added
  1743.      for us (it didn't know yet if the fn was static or not).  */
  1744.       DECL_ARGUMENTS (olddecl) = TREE_CHAIN (DECL_ARGUMENTS (olddecl));
  1745.       DECL_ARGUMENTS (fndecl) = TREE_CHAIN (DECL_ARGUMENTS (fndecl));
  1746.     }
  1747.      
  1748.   t = DECL_TEMPLATE_INFO (tmpl);
  1749.  
  1750.   /* If we have a preexisting version of this function, don't expand
  1751.      the template version, use the other instead.  */
  1752.   if (TREE_STATIC (fndecl))
  1753.     {
  1754.       SET_DECL_TEMPLATE_SPECIALIZATION (fndecl);
  1755.       p = (struct pending_inline *)0;
  1756.     }
  1757.   else if (t->text)
  1758.     {
  1759.       SET_DECL_IMPLICIT_INSTANTIATION (fndecl);
  1760.       repo_template_used (fndecl);
  1761.       p = (struct pending_inline *) permalloc (sizeof (struct pending_inline));
  1762.       p->parm_vec = t->parm_vec;
  1763.       p->bindings = targs;
  1764.       p->can_free = 0;
  1765.       p->deja_vu = 0;
  1766.       p->buf = t->text;
  1767.       p->len = t->length;
  1768.       p->fndecl = fndecl;
  1769.       {
  1770.     int l = lineno;
  1771.     char * f = input_filename;
  1772.  
  1773.     lineno = p->lineno = t->lineno;
  1774.     input_filename = p->filename = t->filename;
  1775.  
  1776.     extract_interface_info ();
  1777.  
  1778.     if (interface_unknown && flag_external_templates)
  1779.       {
  1780.         if (DECL_CLASS_CONTEXT (fndecl)
  1781.         && CLASSTYPE_INTERFACE_KNOWN (DECL_CLASS_CONTEXT (fndecl)))
  1782.           {
  1783.         interface_unknown = 0;
  1784.         interface_only
  1785.           = CLASSTYPE_INTERFACE_ONLY (DECL_CLASS_CONTEXT (fndecl));
  1786.           }
  1787.         else if (! DECL_IN_SYSTEM_HEADER (tmpl))
  1788.           warn_if_unknown_interface (tmpl);
  1789.       }
  1790.  
  1791.     if (interface_unknown || ! flag_external_templates)
  1792.       p->interface = 1;        /* unknown */
  1793.     else
  1794.       p->interface = interface_only ? 0 : 2;
  1795.  
  1796.     lineno = l;
  1797.     input_filename = f;
  1798.  
  1799.     extract_interface_info ();
  1800.       }
  1801.     }
  1802.   else
  1803.     p = (struct pending_inline *)0;
  1804.  
  1805.   DECL_TEMPLATE_INSTANTIATIONS (tmpl) =
  1806.     tree_cons (targs, fndecl, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
  1807.  
  1808.   if (p == (struct pending_inline *)0)
  1809.     {
  1810.       /* do nothing */
  1811.     }
  1812.   else if (DECL_INLINE (fndecl))
  1813.     {
  1814.       DECL_PENDING_INLINE_INFO (fndecl) = p;
  1815.       p->next = pending_inlines;
  1816.       pending_inlines = p;
  1817.     }
  1818.   else
  1819.     {
  1820.       p->next = pending_template_expansions;
  1821.       pending_template_expansions = p;
  1822.     }
  1823.  exit:
  1824.   function_maybepermanent_obstack = old_fmp_obstack;
  1825.   pop_obstacks ();
  1826.  
  1827.   return fndecl;
  1828. }
  1829.  
  1830. /* classlevel should now never be true.  jason 4/12/94 */
  1831. void
  1832. undo_template_name_overload (id, classlevel)
  1833.      tree id;
  1834.      int classlevel;
  1835. {
  1836.   tree template;
  1837.  
  1838.   template = IDENTIFIER_TEMPLATE (id);
  1839.   if (!template)
  1840.     return;
  1841.  
  1842. #if 0 /* not yet, should get fixed properly later */
  1843.   poplevel (0, 0, 0);
  1844. #endif
  1845. #if 1 /* XXX */
  1846.   /* This was a botch... See `overload_template_name' just below.  */
  1847.   if (!classlevel)
  1848.     poplevel (0, 0, 0);
  1849. #endif
  1850. }
  1851.  
  1852. /* classlevel should now never be true.  jason 4/12/94 */
  1853. void
  1854. overload_template_name (id, classlevel)
  1855.      tree id;
  1856.      int classlevel;
  1857. {
  1858.   tree template, t, decl;
  1859.   struct template_info *tinfo;
  1860.  
  1861.   my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 284);
  1862.   template = IDENTIFIER_TEMPLATE (id);
  1863.   if (!template)
  1864.     return;
  1865.  
  1866.   template = TREE_PURPOSE (template);
  1867.   tinfo = DECL_TEMPLATE_INFO (template);
  1868.   template = DECL_NAME (template);
  1869.   my_friendly_assert (template != NULL_TREE, 285);
  1870.  
  1871. #if 1 /* XXX */
  1872.   /* This was a botch... names of templates do not get their own private
  1873.      scopes.  Rather, they should go into the binding level already created
  1874.      by push_template_decls.  Except that there isn't one of those for
  1875.      specializations.  */
  1876.   if (!classlevel)
  1877.     {
  1878.       pushlevel (1);
  1879.       declare_pseudo_global_level ();
  1880.     }
  1881. #endif
  1882.  
  1883.   t = xref_tag (tinfo->aggr, id, NULL_TREE, 1);
  1884.   my_friendly_assert (TREE_CODE (t) == RECORD_TYPE
  1885.               || TREE_CODE (t) == UNION_TYPE
  1886.               || TREE_CODE (t) == UNINSTANTIATED_P_TYPE, 286);
  1887.  
  1888.   decl = build_decl (TYPE_DECL, template, t);
  1889.   SET_DECL_ARTIFICIAL (decl);
  1890.  
  1891. #if 0 /* fix this later */
  1892.   /* We don't want to call here if the work has already been done.  */
  1893.   t = (classlevel
  1894.        ? IDENTIFIER_CLASS_VALUE (template)
  1895.        : IDENTIFIER_LOCAL_VALUE (template));
  1896.   if (t
  1897.       && TREE_CODE (t) == TYPE_DECL
  1898.       && TREE_TYPE (t) == t)
  1899.     my_friendly_abort (85);
  1900. #endif
  1901.  
  1902.   if (classlevel)
  1903.     pushdecl_class_level (decl);
  1904.   else
  1905.     pushdecl (decl);
  1906.  
  1907. #if 0 /* This seems bogus to me; if it isn't, explain why.  (jason) */
  1908.   /* Fake this for now, just to make dwarfout.c happy.  It will have to
  1909.      be done in a proper way later on.  */
  1910.   DECL_CONTEXT (decl) = t;
  1911. #endif
  1912. }
  1913.  
  1914. /* NAME is the IDENTIFIER value of a PRE_PARSED_CLASS_DECL. */
  1915. void
  1916. end_template_instantiation (name)
  1917.      tree name;
  1918. {
  1919.   extern struct pending_input *to_be_restored;
  1920.   tree t, decl;
  1921.  
  1922.   processing_template_defn--;
  1923.   if (!flag_external_templates)
  1924.     interface_unknown--;
  1925.  
  1926.   /* Restore the old parser input state.  */
  1927.   if (yychar == YYEMPTY)
  1928.     yychar = yylex ();
  1929.   if (yychar != END_OF_SAVED_INPUT)
  1930.     error ("parse error at end of class template");
  1931.   else
  1932.     {
  1933.       restore_pending_input (to_be_restored);
  1934.       to_be_restored = 0;
  1935.     }
  1936.  
  1937.   /* Our declarations didn't get stored in the global slot, since
  1938.      there was a (supposedly tags-transparent) scope in between.  */
  1939.   t = IDENTIFIER_TYPE_VALUE (name);
  1940.   my_friendly_assert (t != NULL_TREE
  1941.               && TREE_CODE_CLASS (TREE_CODE (t)) == 't',
  1942.               287);
  1943.   SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
  1944.   /* Make methods of template classes static, unless
  1945.      -fexternal-templates is given.  */
  1946.   if (!flag_external_templates)
  1947.     SET_CLASSTYPE_INTERFACE_UNKNOWN (t);
  1948.   decl = IDENTIFIER_GLOBAL_VALUE (name);
  1949.   my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 288);
  1950.  
  1951.   undo_template_name_overload (name, 0);
  1952.   t = IDENTIFIER_TEMPLATE (name);
  1953.   pop_template_decls (DECL_TEMPLATE_PARMS (TREE_PURPOSE (t)), TREE_VALUE (t),
  1954.               0);
  1955.   /* This will fix up the type-value field.  */
  1956.   pushdecl (decl);
  1957.   pop_from_top_level ();
  1958.  
  1959. #ifdef DWARF_DEBUGGING_INFO
  1960.   if (write_symbols == DWARF_DEBUG && TREE_CODE (decl) == TYPE_DECL)
  1961.     {
  1962.       /* We just completed the definition of a new file-scope type,
  1963.      so we can go ahead and output debug-info for it now.  */
  1964.       TYPE_STUB_DECL (TREE_TYPE (decl)) = decl;
  1965.       rest_of_type_compilation (TREE_TYPE (decl), 1);
  1966.     }
  1967. #endif /* DWARF_DEBUGGING_INFO */
  1968.  
  1969.   /* Restore interface/implementation settings.     */
  1970.   extract_interface_info ();
  1971. }
  1972.  
  1973. /* Store away the text of an template.  */
  1974.  
  1975. void
  1976. reinit_parse_for_template (yychar, d1, d2)
  1977.      int yychar;
  1978.      tree d1, d2;
  1979. {
  1980.   struct template_info *template_info;
  1981.   extern struct obstack inline_text_obstack; /* see comment in lex.c */
  1982.  
  1983.   if (d2 == NULL_TREE || d2 == error_mark_node)
  1984.     {
  1985.     lose:
  1986.       /* @@ Should use temp obstack, and discard results.  */
  1987.       reinit_parse_for_block (yychar, &inline_text_obstack, 1);
  1988.       return;
  1989.     }
  1990.  
  1991.   if (TREE_CODE (d2) == IDENTIFIER_NODE)
  1992.     d2 = IDENTIFIER_GLOBAL_VALUE (d2);
  1993.   if (!d2)
  1994.     goto lose;
  1995.   template_info = DECL_TEMPLATE_INFO (d2);
  1996.   if (!template_info)
  1997.     {
  1998.       template_info = (struct template_info *) permalloc (sizeof (struct template_info));
  1999.       bzero ((char *) template_info, sizeof (struct template_info));
  2000.       DECL_TEMPLATE_INFO (d2) = template_info;
  2001.     }
  2002.   template_info->filename = input_filename;
  2003.   template_info->lineno = lineno;
  2004.   reinit_parse_for_block (yychar, &inline_text_obstack, 1);
  2005.   template_info->text = obstack_base (&inline_text_obstack);
  2006.   template_info->length = obstack_object_size (&inline_text_obstack);
  2007.   obstack_finish (&inline_text_obstack);
  2008.   template_info->parm_vec = d1;
  2009. }
  2010.  
  2011. /* Type unification.
  2012.  
  2013.    We have a function template signature with one or more references to
  2014.    template parameters, and a parameter list we wish to fit to this
  2015.    template.  If possible, produce a list of parameters for the template
  2016.    which will cause it to fit the supplied parameter list.
  2017.  
  2018.    Return zero for success, 2 for an incomplete match that doesn't resolve
  2019.    all the types, and 1 for complete failure.  An error message will be
  2020.    printed only for an incomplete match.
  2021.  
  2022.    TPARMS[NTPARMS] is an array of template parameter types;
  2023.    TARGS[NTPARMS] is the array of template parameter values.  PARMS is
  2024.    the function template's signature (using TEMPLATE_PARM_IDX nodes),
  2025.    and ARGS is the argument list we're trying to match against it.
  2026.  
  2027.    If SUBR is 1, we're being called recursively (to unify the arguments of
  2028.    a function or method parameter of a function template), so don't zero
  2029.    out targs and don't fail on an incomplete match. */
  2030.  
  2031. int
  2032. type_unification (tparms, targs, parms, args, nsubsts, subr)
  2033.      tree tparms, *targs, parms, args;
  2034.      int *nsubsts, subr;
  2035. {
  2036.   tree parm, arg;
  2037.   int i;
  2038.   int ntparms = TREE_VEC_LENGTH (tparms);
  2039.  
  2040.   my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289);
  2041.   my_friendly_assert (TREE_CODE (parms) == TREE_LIST, 290);
  2042.   /* ARGS could be NULL (via a call from parse.y to
  2043.      build_x_function_call).  */
  2044.   if (args)
  2045.     my_friendly_assert (TREE_CODE (args) == TREE_LIST, 291);
  2046.   my_friendly_assert (ntparms > 0, 292);
  2047.  
  2048.   if (!subr)
  2049.     bzero ((char *) targs, sizeof (tree) * ntparms);
  2050.  
  2051.   while (parms
  2052.      && parms != void_list_node
  2053.      && args
  2054.      && args != void_list_node)
  2055.     {
  2056.       parm = TREE_VALUE (parms);
  2057.       parms = TREE_CHAIN (parms);
  2058.       arg = TREE_VALUE (args);
  2059.       args = TREE_CHAIN (args);
  2060.  
  2061.       if (arg == error_mark_node)
  2062.     return 1;
  2063.       if (arg == unknown_type_node)
  2064.     return 1;
  2065.  
  2066.       if (! uses_template_parms (parm)
  2067.       && TREE_CODE_CLASS (TREE_CODE (arg)) != 't')
  2068.     {
  2069.       if (can_convert_arg (parm, TREE_TYPE (arg), arg))
  2070.         continue;
  2071.       return 1;
  2072.     }
  2073.     
  2074. #if 0
  2075.       if (TREE_CODE (arg) == VAR_DECL)
  2076.     arg = TREE_TYPE (arg);
  2077.       else if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'e')
  2078.     arg = TREE_TYPE (arg);
  2079. #else
  2080.       if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't')
  2081.     {
  2082.       my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293);
  2083.       if (TREE_CODE (arg) == TREE_LIST
  2084.           && TREE_TYPE (arg) == unknown_type_node
  2085.           && TREE_CODE (TREE_VALUE (arg)) == TEMPLATE_DECL)
  2086.         {
  2087.           int nsubsts, ntparms;
  2088.           tree *targs;
  2089.  
  2090.           /* Have to back unify here */
  2091.           arg = TREE_VALUE (arg);
  2092.           nsubsts = 0;
  2093.           ntparms = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (arg));
  2094.           targs = (tree *) alloca (sizeof (tree) * ntparms);
  2095.           parm = tree_cons (NULL_TREE, parm, NULL_TREE);
  2096.           return type_unification (DECL_TEMPLATE_PARMS (arg), targs,
  2097.                        TYPE_ARG_TYPES (TREE_TYPE (arg)),
  2098.                        parm, &nsubsts, 0);
  2099.         }
  2100.       arg = TREE_TYPE (arg);
  2101.     }
  2102. #endif
  2103.       if (TREE_CODE (arg) == REFERENCE_TYPE)
  2104.     arg = TREE_TYPE (arg);
  2105.  
  2106.       if (TREE_CODE (parm) != REFERENCE_TYPE)
  2107.     {
  2108.       if (TREE_CODE (arg) == FUNCTION_TYPE
  2109.           || TREE_CODE (arg) == METHOD_TYPE)
  2110.         arg = build_pointer_type (arg);
  2111.       else if (TREE_CODE (arg) == ARRAY_TYPE)
  2112.         arg = build_pointer_type (TREE_TYPE (arg));
  2113.       else
  2114.         arg = TYPE_MAIN_VARIANT (arg);
  2115.     }
  2116.  
  2117.       switch (unify (tparms, targs, ntparms, parm, arg, nsubsts))
  2118.     {
  2119.     case 0:
  2120.       break;
  2121.     case 1:
  2122.       return 1;
  2123.     }
  2124.     }
  2125.   /* Fail if we've reached the end of the parm list, and more args
  2126.      are present, and the parm list isn't variadic.  */
  2127.   if (args && args != void_list_node && parms == void_list_node)
  2128.     return 1;
  2129.   /* Fail if parms are left and they don't have default values.     */
  2130.   if (parms
  2131.       && parms != void_list_node
  2132.       && TREE_PURPOSE (parms) == NULL_TREE)
  2133.     return 1;
  2134.   if (!subr)
  2135.     for (i = 0; i < ntparms; i++)
  2136.       if (!targs[i])
  2137.     {
  2138.       error ("incomplete type unification");
  2139.       return 2;
  2140.     }
  2141.   return 0;
  2142. }
  2143.  
  2144. /* Tail recursion is your friend.  */
  2145. static int
  2146. unify (tparms, targs, ntparms, parm, arg, nsubsts)
  2147.      tree tparms, *targs, parm, arg;
  2148.      int *nsubsts, ntparms;
  2149. {
  2150.   int idx;
  2151.  
  2152.   /* I don't think this will do the right thing with respect to types.
  2153.      But the only case I've seen it in so far has been array bounds, where
  2154.      signedness is the only information lost, and I think that will be
  2155.      okay.  */
  2156.   while (TREE_CODE (parm) == NOP_EXPR)
  2157.     parm = TREE_OPERAND (parm, 0);
  2158.  
  2159.   if (arg == error_mark_node)
  2160.     return 1;
  2161.   if (arg == unknown_type_node)
  2162.     return 1;
  2163.   if (arg == parm)
  2164.     return 0;
  2165.  
  2166.   switch (TREE_CODE (parm))
  2167.     {
  2168.     case TEMPLATE_TYPE_PARM:
  2169.       (*nsubsts)++;
  2170.       if (TEMPLATE_TYPE_TPARMLIST (parm) != tparms)
  2171.     {
  2172.       error ("mixed template headers?!");
  2173.       my_friendly_abort (86);
  2174.       return 1;
  2175.     }
  2176.       idx = TEMPLATE_TYPE_IDX (parm);
  2177. #if 0
  2178.       /* Template type parameters cannot contain cv-quals; i.e.
  2179.          template <class T> void f (T& a, T& b) will not generate
  2180.      void f (const int& a, const int& b).  */
  2181.       if (TYPE_READONLY (arg) > TYPE_READONLY (parm)
  2182.       || TYPE_VOLATILE (arg) > TYPE_VOLATILE (parm))
  2183.     return 1;
  2184.       arg = TYPE_MAIN_VARIANT (arg);
  2185. #else
  2186.       {
  2187.     int constp = TYPE_READONLY (arg) > TYPE_READONLY (parm);
  2188.     int volatilep = TYPE_VOLATILE (arg) > TYPE_VOLATILE (parm);
  2189.     arg = cp_build_type_variant (arg, constp, volatilep);
  2190.       }
  2191. #endif
  2192.       /* Simple cases: Value already set, does match or doesn't.  */
  2193.       if (targs[idx] == arg)
  2194.     return 0;
  2195.       else if (targs[idx])
  2196.     return 1;
  2197.       /* Check for mixed types and values.  */
  2198.       if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (tparms, idx))) != TYPE_DECL)
  2199.     return 1;
  2200.       targs[idx] = arg;
  2201.       return 0;
  2202.     case TEMPLATE_CONST_PARM:
  2203.       (*nsubsts)++;
  2204.       idx = TEMPLATE_CONST_IDX (parm);
  2205.       if (targs[idx] == arg)
  2206.     return 0;
  2207.       else if (targs[idx])
  2208.     {
  2209.       tree t = targs[idx];
  2210.       if (TREE_CODE (t) == TREE_CODE (arg))
  2211.         switch (TREE_CODE (arg))
  2212.           {
  2213.           case INTEGER_CST:
  2214.         if (tree_int_cst_equal (t, arg))
  2215.           return 0;
  2216.         break;
  2217.           case REAL_CST:
  2218.         if (REAL_VALUES_EQUAL (TREE_REAL_CST (t), TREE_REAL_CST (arg)))
  2219.           return 0;
  2220.         break;
  2221.           /* STRING_CST values are not valid template const parms.  */
  2222.           default:
  2223.         ;
  2224.           }
  2225.       my_friendly_abort (87);
  2226.       return 1;
  2227.     }
  2228. /*    else if (typeof arg != tparms[idx])
  2229.     return 1;*/
  2230.  
  2231.       targs[idx] = copy_to_permanent (arg);
  2232.       return 0;
  2233.  
  2234.     case POINTER_TYPE:
  2235.       if (TREE_CODE (arg) != POINTER_TYPE)
  2236.     return 1;
  2237.       return unify (tparms, targs, ntparms, TREE_TYPE (parm), TREE_TYPE (arg),
  2238.             nsubsts);
  2239.  
  2240.     case REFERENCE_TYPE:
  2241.       if (TREE_CODE (arg) == REFERENCE_TYPE)
  2242.     arg = TREE_TYPE (arg);
  2243.       return unify (tparms, targs, ntparms, TREE_TYPE (parm), arg, nsubsts);
  2244.  
  2245.     case ARRAY_TYPE:
  2246.       if (TREE_CODE (arg) != ARRAY_TYPE)
  2247.     return 1;
  2248.       if (unify (tparms, targs, ntparms, TYPE_DOMAIN (parm), TYPE_DOMAIN (arg),
  2249.          nsubsts) != 0)
  2250.     return 1;
  2251.       return unify (tparms, targs, ntparms, TREE_TYPE (parm), TREE_TYPE (arg),
  2252.             nsubsts);
  2253.  
  2254.     case REAL_TYPE:
  2255.     case INTEGER_TYPE:
  2256.       if (TREE_CODE (arg) != TREE_CODE (parm))
  2257.     return 1;
  2258.  
  2259.       if (TREE_CODE (parm) == INTEGER_TYPE)
  2260.     {
  2261.       if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg)
  2262.           && unify (tparms, targs, ntparms,
  2263.             TYPE_MIN_VALUE (parm), TYPE_MIN_VALUE (arg), nsubsts))
  2264.         return 1;
  2265.       if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg)
  2266.           && unify (tparms, targs, ntparms,
  2267.             TYPE_MAX_VALUE (parm), TYPE_MAX_VALUE (arg), nsubsts))
  2268.         return 1;
  2269.     }
  2270.       /* As far as unification is concerned, this wins.     Later checks
  2271.      will invalidate it if necessary.  */
  2272.       return 0;
  2273.  
  2274.       /* Types INTEGER_CST and MINUS_EXPR can come from array bounds.  */
  2275.     case INTEGER_CST:
  2276.       if (TREE_CODE (arg) != INTEGER_CST)
  2277.     return 1;
  2278.       return !tree_int_cst_equal (parm, arg);
  2279.  
  2280.     case MINUS_EXPR:
  2281.       {
  2282.     tree t1, t2;
  2283.     t1 = TREE_OPERAND (parm, 0);
  2284.     t2 = TREE_OPERAND (parm, 1);
  2285.     return unify (tparms, targs, ntparms, t1,
  2286.               fold (build (PLUS_EXPR, integer_type_node, arg, t2)),
  2287.               nsubsts);
  2288.       }
  2289.  
  2290.     case TREE_VEC:
  2291.       {
  2292.     int i;
  2293.     if (TREE_CODE (arg) != TREE_VEC)
  2294.       return 1;
  2295.     if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
  2296.       return 1;
  2297.     for (i = TREE_VEC_LENGTH (parm) - 1; i >= 0; i--)
  2298.       if (unify (tparms, targs, ntparms,
  2299.              TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
  2300.              nsubsts))
  2301.         return 1;
  2302.     return 0;
  2303.       }
  2304.  
  2305.     case UNINSTANTIATED_P_TYPE:
  2306.       {
  2307.     tree a;
  2308.     /* Unification of something that is not a class fails.  */
  2309.     if (! IS_AGGR_TYPE (arg))
  2310.       return 1;
  2311.     a = IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (arg));
  2312.     if (a && UPT_TEMPLATE (parm) == TREE_PURPOSE (a))
  2313.       return unify (tparms, targs, ntparms, UPT_PARMS (parm),
  2314.             TREE_VALUE (a), nsubsts);
  2315.     /* FIXME: Should check base conversions here.  */
  2316.     return 1;
  2317.       }
  2318.  
  2319.     case RECORD_TYPE:
  2320.       if (TYPE_PTRMEMFUNC_FLAG (parm))
  2321.     return unify (tparms, targs, ntparms, TYPE_PTRMEMFUNC_FN_TYPE (parm),
  2322.               arg, nsubsts);
  2323.  
  2324.       /* Allow trivial conversions.  */
  2325.       if (TYPE_MAIN_VARIANT (parm) != TYPE_MAIN_VARIANT (arg)
  2326.       || TYPE_READONLY (parm) < TYPE_READONLY (arg)
  2327.       || TYPE_VOLATILE (parm) < TYPE_VOLATILE (arg))
  2328.     return 1;
  2329.       return 0;
  2330.  
  2331.     case METHOD_TYPE:
  2332.       if (TREE_CODE (arg) != METHOD_TYPE)
  2333.     return 1;
  2334.       goto check_args;
  2335.  
  2336.     case FUNCTION_TYPE:
  2337.       if (TREE_CODE (arg) != FUNCTION_TYPE)
  2338.     return 1;
  2339.      check_args:
  2340.       if (unify (tparms, targs, ntparms, TREE_TYPE (parm),
  2341.          TREE_TYPE (arg), nsubsts))
  2342.     return 1;
  2343.       return type_unification (tparms, targs, TYPE_ARG_TYPES (parm),
  2344.                    TYPE_ARG_TYPES (arg), nsubsts, 1);
  2345.  
  2346.     case OFFSET_TYPE:
  2347.       if (TREE_CODE (arg) != OFFSET_TYPE)
  2348.     return 1;
  2349.       if (unify (tparms, targs, ntparms, TYPE_OFFSET_BASETYPE (parm),
  2350.          TYPE_OFFSET_BASETYPE (arg), nsubsts))
  2351.     return 1;
  2352.       return unify (tparms, targs, ntparms, TREE_TYPE (parm),
  2353.             TREE_TYPE (arg), nsubsts);
  2354.  
  2355.     default:
  2356.       sorry ("use of `%s' in template type unification",
  2357.          tree_code_name [(int) TREE_CODE (parm)]);
  2358.       return 1;
  2359.     }
  2360. }
  2361.  
  2362.  
  2363. #undef DEBUG
  2364.  
  2365. int
  2366. do_pending_expansions ()
  2367. {
  2368.   struct pending_inline *i, *new_list = 0;
  2369.  
  2370.   if (!pending_template_expansions)
  2371.     return 0;
  2372.  
  2373. #ifdef DEBUG
  2374.   fprintf (stderr, "\n\n\t\t IN DO_PENDING_EXPANSIONS\n\n");
  2375. #endif
  2376.  
  2377.   i = pending_template_expansions;
  2378.   while (i)
  2379.     {
  2380.       tree context;
  2381.  
  2382.       struct pending_inline *next = i->next;
  2383.       tree t = i->fndecl;
  2384.  
  2385.       int decision = 0;
  2386. #define DECIDE(N) do {decision=(N); goto decided;} while(0)
  2387.  
  2388.       my_friendly_assert (TREE_CODE (t) == FUNCTION_DECL
  2389.               || TREE_CODE (t) == VAR_DECL, 294);
  2390.       if (TREE_ASM_WRITTEN (t))
  2391.     DECIDE (0);
  2392.  
  2393.       if (DECL_EXPLICIT_INSTANTIATION (t))
  2394.     DECIDE (DECL_NOT_REALLY_EXTERN (t));
  2395.       else if (! flag_implicit_templates)
  2396.     DECIDE (0);
  2397.  
  2398.       if (i->interface == 1)
  2399.     /* OK, it was an implicit instantiation.  */
  2400.     {
  2401.       if (SUPPORTS_WEAK)
  2402.         DECL_WEAK (t) = 1;
  2403.       else
  2404.         TREE_PUBLIC (t) = 0;
  2405.     }
  2406.  
  2407.       /* If it's a method, let the class type decide it.
  2408.      @@ What if the method template is in a separate file?
  2409.      Maybe both file contexts should be taken into account?
  2410.      Maybe only do this if i->interface == 1 (unknown)?  */
  2411.       context = DECL_CONTEXT (t);
  2412.       if (context != NULL_TREE
  2413.       && TREE_CODE_CLASS (TREE_CODE (context)) == 't')
  2414.     {
  2415.       /* I'm interested in the context of this version of the function,
  2416.          not the original virtual declaration.  */
  2417.       context = DECL_CLASS_CONTEXT (t);
  2418.  
  2419.       /* If `unknown', we might want a static copy.
  2420.          If `implementation', we want a global one.
  2421.          If `interface', ext ref.  */
  2422.       if (CLASSTYPE_INTERFACE_KNOWN (context))
  2423.         DECIDE (!CLASSTYPE_INTERFACE_ONLY (context));
  2424. #if 1 /* This doesn't get us stuff needed only by the file initializer.  */
  2425.       DECIDE (TREE_USED (t));
  2426. #else /* This compiles too much stuff, but that's probably better in
  2427.      most cases than never compiling the stuff we need.  */
  2428.       DECIDE (1);
  2429. #endif
  2430.     }
  2431.  
  2432.       if (i->interface == 1)
  2433.     DECIDE (TREE_USED (t));
  2434.       else
  2435.     DECIDE (i->interface);
  2436.  
  2437.     decided:
  2438. #ifdef DEBUG
  2439.       print_node_brief (stderr, decision ? "yes: " : "no: ", t, 0);
  2440.       fprintf (stderr, "\t%s\n",
  2441.            (DECL_ASSEMBLER_NAME (t)
  2442.         ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t))
  2443.         : ""));
  2444. #endif
  2445.       if (decision)
  2446.     {
  2447.       i->next = pending_inlines;
  2448.       pending_inlines = i;
  2449.     }
  2450.       else
  2451.     {
  2452.       i->next = new_list;
  2453.       new_list = i;
  2454.     }
  2455.       i = next;
  2456.     }
  2457.   pending_template_expansions = new_list;
  2458.   if (!pending_inlines)
  2459.     return 0;
  2460.   do_pending_inlines ();
  2461.   return 1;
  2462. }
  2463.  
  2464.  
  2465. struct pending_template {
  2466.   struct pending_template *next;
  2467.   tree id;
  2468. };
  2469.  
  2470. static struct pending_template* pending_templates;
  2471.  
  2472. void
  2473. do_pending_templates ()
  2474. {
  2475.   struct pending_template* t;
  2476.   
  2477.   for ( t = pending_templates; t; t = t->next)
  2478.     {
  2479.       instantiate_class_template (t->id, 1);
  2480.     }
  2481.  
  2482.   for ( t = pending_templates; t; t = pending_templates)
  2483.     {
  2484.       pending_templates = t->next;
  2485.       free(t);
  2486.     }
  2487. }
  2488.  
  2489. static void
  2490. add_pending_template (pt)
  2491.      tree pt;
  2492. {
  2493.   struct pending_template *p;
  2494.   
  2495.   p = (struct pending_template *) malloc (sizeof (struct pending_template));
  2496.   p->next = pending_templates;
  2497.   pending_templates = p;
  2498.   p->id = pt;
  2499. }
  2500.  
  2501. void
  2502. mark_function_instantiated (result, extern_p)
  2503.      tree result;
  2504.      int extern_p;
  2505. {
  2506.   if (DECL_TEMPLATE_INSTANTIATION (result))
  2507.     SET_DECL_EXPLICIT_INSTANTIATION (result);
  2508.   TREE_PUBLIC (result) = 1;
  2509.  
  2510.   if (! extern_p)
  2511.     {
  2512.       DECL_INTERFACE_KNOWN (result) = 1;
  2513.       DECL_NOT_REALLY_EXTERN (result) = 1;
  2514.     }
  2515. }
  2516.  
  2517. /* called from the parser.  */
  2518. void
  2519. do_function_instantiation (declspecs, declarator, storage)
  2520.      tree declspecs, declarator, storage;
  2521. {
  2522.   tree decl = grokdeclarator (declarator, declspecs, NORMAL, 0, 0);
  2523.   tree name;
  2524.   tree fn;
  2525.   tree result = NULL_TREE;
  2526.   int extern_p = 0;
  2527.  
  2528.   /* If we've already seen this template instance, use it.  */
  2529.   if (name = DECL_ASSEMBLER_NAME (decl),
  2530.       fn = IDENTIFIER_GLOBAL_VALUE (name),
  2531.       fn && DECL_TEMPLATE_INSTANTIATION (fn))
  2532.     result = fn;
  2533.   else if (name = DECL_NAME (decl), fn = IDENTIFIER_GLOBAL_VALUE (name), fn)
  2534.     {
  2535.       for (fn = get_first_fn (fn); fn; fn = DECL_CHAIN (fn))
  2536.     if (decls_match (fn, decl)
  2537.         && DECL_DEFER_OUTPUT (fn))
  2538.       {
  2539.         result = fn;
  2540.         break;
  2541.       }
  2542.     else if (TREE_CODE (fn) == TEMPLATE_DECL)
  2543.       {
  2544.         int ntparms = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (fn));
  2545.         tree *targs = (tree *) malloc (sizeof (tree) * ntparms);
  2546.         int i, dummy = 0;
  2547.         i = type_unification (DECL_TEMPLATE_PARMS (fn), targs,
  2548.                   TYPE_ARG_TYPES (TREE_TYPE (fn)),
  2549.                   TYPE_ARG_TYPES (TREE_TYPE (decl)),
  2550.                   &dummy, 0);
  2551.         if (i == 0)
  2552.           {
  2553.         if (result)
  2554.           cp_error ("ambiguous template instantiation for `%D' requested", decl);
  2555.         else
  2556.           result = instantiate_template (fn, targs);
  2557.           }
  2558.         free (targs);
  2559.       }
  2560.     }
  2561.   if (! result)
  2562.     {
  2563.       cp_error ("no matching template for `%D' found", decl);
  2564.       return;
  2565.     }
  2566.  
  2567.   if (flag_external_templates)
  2568.     return;
  2569.  
  2570.   if (storage == NULL_TREE)
  2571.     ;
  2572.   else if (storage == ridpointers[(int) RID_EXTERN])
  2573.     extern_p = 1;
  2574.   else
  2575.     cp_error ("storage class `%D' applied to template instantiation",
  2576.           storage);
  2577.   mark_function_instantiated (result, extern_p);
  2578.   repo_template_instantiated (result, extern_p);
  2579. }
  2580.  
  2581. void
  2582. mark_class_instantiated (t, extern_p)
  2583.      tree t;
  2584.      int extern_p;
  2585. {
  2586.   SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
  2587.   SET_CLASSTYPE_INTERFACE_KNOWN (t);
  2588.   CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
  2589.   CLASSTYPE_VTABLE_NEEDS_WRITING (t) = ! extern_p;
  2590.   TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
  2591.   if (! extern_p)
  2592.     {
  2593.       CLASSTYPE_DEBUG_REQUESTED (t) = 1;
  2594.       rest_of_type_compilation (t, 1);
  2595.     }
  2596. }     
  2597.  
  2598. void
  2599. do_type_instantiation (name, storage)
  2600.      tree name, storage;
  2601. {
  2602.   tree t = TREE_TYPE (name);
  2603.   int extern_p = 0;
  2604.   int nomem_p = 0;
  2605.  
  2606.   /* With -fexternal-templates, explicit instantiations are treated the same
  2607.      as implicit ones.  */
  2608.   if (flag_external_templates)
  2609.     return;
  2610.  
  2611.   if (TYPE_SIZE (t) == NULL_TREE)
  2612.     {
  2613.       cp_error ("explicit instantiation of `%#T' before definition of template",
  2614.         t);
  2615.       return;
  2616.     }
  2617.  
  2618.   if (storage == NULL_TREE)
  2619.     /* OK */;
  2620.   else if (storage == ridpointers[(int) RID_INLINE])
  2621.     nomem_p = 1;
  2622.   else if (storage == ridpointers[(int) RID_EXTERN])
  2623.     extern_p = 1;
  2624.   else
  2625.     {
  2626.       cp_error ("storage class `%D' applied to template instantiation",
  2627.         storage);
  2628.       extern_p = 0;
  2629.     }
  2630.  
  2631.   /* We've already instantiated this.  */
  2632.   if (CLASSTYPE_EXPLICIT_INSTANTIATION (t) && ! CLASSTYPE_INTERFACE_ONLY (t)
  2633.       && extern_p)
  2634.     return;
  2635.  
  2636.   if (! CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
  2637.     {
  2638.       mark_class_instantiated (t, extern_p);
  2639.       repo_template_instantiated (t, extern_p);
  2640.     }
  2641.  
  2642.   if (nomem_p)
  2643.     return;
  2644.  
  2645.   {
  2646.     tree tmp;
  2647.     /* Classes nested in template classes currently don't have an
  2648.        IDENTIFIER_TEMPLATE--their out-of-line members are handled
  2649.        by the enclosing template class.  Note that there are name
  2650.        conflict bugs with this approach. */
  2651.     tmp = TYPE_IDENTIFIER (t);
  2652.     if (IDENTIFIER_TEMPLATE (tmp))
  2653.       instantiate_member_templates (tmp);
  2654.  
  2655.     /* this should really be done by instantiate_member_templates */
  2656.     tmp = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 0);
  2657.     for (; tmp; tmp = TREE_CHAIN (tmp))
  2658.       if (DECL_TEMPLATE_INSTANTIATION (tmp))
  2659.     {
  2660.       mark_function_instantiated (tmp, extern_p);
  2661.       repo_template_instantiated (tmp, extern_p);
  2662.     }
  2663.  
  2664. #if 0
  2665.     for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
  2666.       {
  2667.     if (TREE_CODE (tmp) == VAR_DECL)
  2668.       /* eventually do something */;
  2669.       }
  2670. #endif
  2671.  
  2672.     for (tmp = CLASSTYPE_TAGS (t); tmp; tmp = TREE_CHAIN (tmp))
  2673.       if (IS_AGGR_TYPE (TREE_VALUE (tmp)))
  2674.     do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp)), storage);
  2675.   }
  2676. }
  2677.  
  2678. tree
  2679. create_nested_upt (scope, name)
  2680.      tree scope, name;
  2681. {
  2682.   tree t = make_lang_type (UNINSTANTIATED_P_TYPE);
  2683.   tree d = build_decl (TYPE_DECL, name, t);
  2684.  
  2685.   TYPE_NAME (t) = d;
  2686.   TYPE_VALUES (t) = TYPE_VALUES (scope);
  2687.   TYPE_CONTEXT (t) = scope;
  2688.  
  2689.   pushdecl (d);
  2690.   return d;
  2691. }
  2692.